conditional rendering in react
JavaScript
import React, { Component } from 'react';
// @params [] * denotes optional param (we will need to use conditional rendering for some of these)
// [type](Bulma CSS class): Hero type, focuses on the base styling
// size(Bulma CSS Class): The size of the hero, small, medium, large, etc...
// heading: The main heading
// [subheading]: The subheading if desired
// [alignment](Bulma CSS Class): Aligns the content horizontally
// This Simple HeroComponent is bases upon the following
// https://bulma.io/documentation/layout/hero/
export class HeroComponent extends Component
{
render() {
return (
// The following ternary simply applies a class if it has been specified
<section className={"hero" + (this.props.type ? " " + this.props.type + " " : " ") + this.props.size}>
<div className="hero-body">
// Again, another ternary applying a class... blah blah blah....
<div className={"container" + this.props.alignment ? " " + this.props.alignment : ""}>
<h1 className="title">{this.props.heading}</h1>
// So, to answer the question...
// The following is one way to do conditional rendering, probably the simplest and cleanest
// If this.props.subheading exists, render <h2 .. />
{this.props.subheading && <h2 className="subtitle">{this.props.subheading}</h2>}
</div>
</div>
</section>
)
}
}function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
<div>
<h1>Hello!</h1>
{unreadMessages.length > 0 &&
<h2>
You have {unreadMessages.length} unread messages.
</h2>
}
</div>
);
}render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in. </div>
);
}lass LoginControl extends React.Component {
constructor(props) {
super(props);
this.handleLoginClick = this.handleLoginClick.bind(this);
this.handleLogoutClick = this.handleLogoutClick.bind(this);
this.state = {isLoggedIn: false};
}
handleLoginClick() {
this.setState({isLoggedIn: true});
}
handleLogoutClick() {
this.setState({isLoggedIn: false});
}
render() {
const isLoggedIn = this.state.isLoggedIn;
let button;
if (isLoggedIn) {
button = <LogoutButton onClick={this.handleLogoutClick} />;
} else {
button = <LoginButton onClick={this.handleLoginClick} />;
}
return (
<div>
<Greeting isLoggedIn={isLoggedIn} />
{button}
</div>
);
}
}
ReactDOM.render(
<LoginControl />,
document.getElementById('root')
);
Also in JavaScript:
- Title
- how to check if object is undefined in javascript
- Category
- JavaScript
- Title
- angular event emitter
- Category
- JavaScript
- Title
- electron Uncaught ReferenceError: require is not defined at recorder.js:1
- Category
- JavaScript
- Title
- array insertion javascript
- Category
- JavaScript
- Title
- how to clone an object in javascript
- Category
- JavaScript
- Title
- base64 decode javascript
- Category
- JavaScript
- Title
- how to get a random element of an array javascript
- Category
- JavaScript
- Title
- function js
- Category
- JavaScript
- Title
- check if localstorage key exists js
- Category
- JavaScript
- Title
- ckeditor get instance from textarea
- Category
- JavaScript
- Title
- array find method javascript
- Category
- JavaScript
- Title
- find least common multiple javascript
- Category
- JavaScript
- Title
- how to play sound on load js
- Category
- JavaScript
- Title
- how do you remove a remove element from array in javascript
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- axios file upload
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- form taglib in jsp
- Category
- JavaScript
- Title
- foreach key value javascript
- Category
- JavaScript
- Title
- iterate through array js
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- how to return the max and min of an array in javascript
- Category
- JavaScript
- Title
- how to read json file in python
- Category
- JavaScript
- Title
- how to send an alert in javascript
- Category
- JavaScript
- Title
- hover jquery
- Category
- JavaScript
- Title
- ajax laravel example
- Category
- JavaScript
- Title
- how to make a page relode on a keypress in js
- Category
- JavaScript
- Title
- get url of page in background script
- Category
- JavaScript
- Title
- arrow function javascript ie11
- Category
- JavaScript
- Title
- how to add a class to html using javascript
- Category
- JavaScript
- Title
- javascript change web page title
- Category
- JavaScript
- Title
- comment in jsp file
- Category
- JavaScript
- Title
- how to validate input type=date in javascript
- Category
- JavaScript
- Title
- how to make one line if in js
- Category
- JavaScript
- Title
- change node version
- Category
- JavaScript
- Title
- array.filter in js
- Category
- JavaScript
- Title
- eyeshot javascript version
- Category
- JavaScript
- Title
- how to validate the radio button using jquery
- Category
- JavaScript
- Title
- .innerhtml
- Category
- JavaScript
- Title
- debug node
- Category
- JavaScript
- Title
- Add disclaimer for best browser experience in angular
- Category
- JavaScript
- Title
- fizz buzz javascript
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- difference between React Native and React
- Category
- JavaScript
- Title
- how to access all images in a document javascript
- Category
- JavaScript
- Title
- installing vue router & implementation
- Category
- JavaScript
- Title
- how to use react memo hooks
- Category
- JavaScript
- Title
- how to rescale a canvas in html5 without blurrriness
- Category
- JavaScript
- Title
- hoisting in javascript
- Category
- JavaScript
- Title
- arrays javascript
- Category
- JavaScript
- Title
- 1 - - 1 in javascript
- Category
- JavaScript
- Title
- call javascript function from html
- Category
- JavaScript
- Title
- get value of checked radio button jquery
- Category
- JavaScript
- Title
- get value onChange from mat-select angular
- Category
- JavaScript
- Title
- collapse in angular 4
- Category
- JavaScript
- Title
- how to loop through array of numbers in javascript
- Category
- JavaScript
- Title
- how to split an array in javascript
- Category
- JavaScript
- Title
- how to see if a web site is useing react
- Category
- JavaScript
- Title
- bind in javascript
- Category
- JavaScript
- Title
- how to limit characters in number input js
- Category
- JavaScript
- Title
- how to take input in javascript in coding
- Category
- JavaScript
- Title
- how run dockerfile
- Category
- JavaScript
- Title
- graphql react filtering
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- jason rpc reactjs
- Category
- JavaScript
- Title
- javascript check if string contains substring
- Category
- JavaScript
- Title
- How to uninstall npm modules in node js?
- Category
- JavaScript
- Title
- javascript adding an array to an array
- Category
- JavaScript
- Title
- angular material toolbar with logo
- Category
- JavaScript
- Title
- bootstrap checkbox
- Category
- JavaScript
- Title
- how to do bubble sort in javascript
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- express get params after ?
- Category
- JavaScript
- Title
- how to make fake binary
- Category
- JavaScript
- Title
- graphql yoga access http headers
- Category
- JavaScript
- Title
- install nuxt js
- Category
- JavaScript
- Title
- get date one week from now javascript
- Category
- JavaScript
- Title
- deny ready jquery
- Category
- JavaScript
- Title
- callback in response node.js
- Category
- JavaScript
- Title
- hover vanilla javascript
- Category
- JavaScript
- Title
- accèder data-id javascript
- Category
- JavaScript
- Title
- async reduce javascript
- Category
- JavaScript
- Title
- ajax open new tab with post
- Category
- JavaScript
- Title
- iframe loader jquery
- Category
- JavaScript
- Title
- javascript add function to onchange event
- Category
- JavaScript
- Title
- how to use react components
- Category
- JavaScript
- Title
- copy one array to another javascript
- Category
- JavaScript
- Title
- d3 disable context menu
- Category
- JavaScript
- Title
- javascript add row to table
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- class in react
- Category
- JavaScript
- Title
- how to count click events javascript
- Category
- JavaScript
- Title
- html get elements by class
- Category
- JavaScript
- Title
- class javascript
- Category
- JavaScript
- Title
- how to make fizzbuzz in javascript
- Category
- JavaScript
- Title
- anagram js
- Category
- JavaScript
- Title
- express limit based on ip
- Category
- JavaScript
- Title
- is checked checkbox jquery
- Category
- JavaScript
- Title
- Angular add class dynamically
- Category
- JavaScript
- Title
- image continuous changing div react
- Category
- JavaScript