conditional rendering react
JavaScript
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>
);
}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>
)
}
}// This JSX:
<div id={if (condition) { 'msg' }}>Hello World!</div>
// Is transformed to this JS:
React.createElement("div", {id: if (condition) { 'msg' }}, "Hello World!");
renderElement(){
if(this.state.value == 'news')
return <Text>data</Text>;
return null;
}
render() {
return (
<View style={styles.container}>
{ this.renderElement() }
</View>
)
}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
- javascrip reverse text
- Category
- JavaScript
- Title
- How to parse POST requests with express nodejs
- Category
- JavaScript
- Title
- javascript array find highest value of array of objects by key
- Category
- JavaScript
- Title
- draw rectangle on javascript canvas
- Category
- JavaScript
- Title
- get last char javascript
- Category
- JavaScript
- Title
- async await in forloops
- Category
- JavaScript
- Title
- for of loop javascript
- Category
- JavaScript
- Title
- How to insert an item into an array at a specific index JavaScript
- Category
- JavaScript
- Title
- javascript change url hash
- Category
- JavaScript
- Title
- insert item into array specific index javascript
- Category
- JavaScript
- Title
- how to use session using javascript
- Category
- JavaScript
- Title
- how to take value from html text box using parseint javascript
- Category
- JavaScript
- Title
- discord.js wait seconds
- Category
- JavaScript
- Title
- How to get row index and cellindex together in javascript
- Category
- JavaScript
- Title
- javascript array unique values
- Category
- JavaScript
- Title
- bootstrap-datepicker disable dates from another month
- Category
- JavaScript
- Title
- href back page javascript
- Category
- JavaScript
- Title
- install node js lts ubuntu 18.04
- Category
- JavaScript
- Title
- how to scroll to an element javascript react
- Category
- JavaScript
- Title
- how to create simple node project
- Category
- JavaScript
- Title
- html javascript redirect
- Category
- JavaScript
- Title
- angularjs iframe src binding
- Category
- JavaScript
- Title
- devextreme custom dxdatagrid field
- Category
- JavaScript
- Title
- how to wait for an exec command to fininsh in nodejs
- Category
- JavaScript
- Title
- how to compare a string with its ending in javascript
- Category
- JavaScript
- Title
- floor in js
- Category
- JavaScript
- Title
- how get parent element javascript
- Category
- JavaScript
- Title
- append data get array
- Category
- JavaScript
- Title
- Finding HTML Elements
- Category
- JavaScript
- Title
- 'ts-node' não é reconhecido como um comando interno ou externo, um programa operável ou um arquivo em lotes.
- Category
- JavaScript
- Title
- int to string javascript
- Category
- JavaScript
- Title
- align left text in reactstrap
- Category
- JavaScript
- Title
- angry professor javascript
- Category
- JavaScript
- Title
- d3 disable context menu
- Category
- JavaScript
- Title
- copy text on button click in jquery
- Category
- JavaScript
- Title
- angular open poort in network
- Category
- JavaScript
- Title
- discord.js reply to message
- Category
- JavaScript
- Title
- difference between foreach and map in js
- Category
- JavaScript
- Title
- get only last 2 digit of year in javascript
- Category
- JavaScript
- Title
- disable inputetext in react native
- Category
- JavaScript
- Title
- font awesome 4.7 cdn
- Category
- JavaScript
- Title
- javascript canvas beziercurveto
- Category
- JavaScript
- Title
- how to link javascript to html
- Category
- JavaScript
- Title
- how to prevent event capturing in javascript
- Category
- JavaScript
- Title
- how to avoid inheritance in angular
- Category
- JavaScript
- Title
- if mousepressed javascript
- Category
- JavaScript
- Title
- fetch api javascript
- Category
- JavaScript
- Title
- javascript clear div
- Category
- JavaScript
- Title
- how to use the javascript console
- Category
- JavaScript
- Title
- convert 24 hour to 12 hour moment js
- Category
- JavaScript
- Title
- get hash js
- Category
- JavaScript
- Title
- character to ascii in js
- Category
- JavaScript
- Title
- google cdn jquery
- Category
- JavaScript
- Title
- for each js
- Category
- JavaScript
- Title
- javascript array push method
- Category
- JavaScript
- Title
- function in react
- Category
- JavaScript
- Title
- javascript check if function exists
- Category
- JavaScript
- Title
- adding bootstrap to angular global styles
- Category
- JavaScript
- Title
- how to make a clock in html/javascript
- Category
- JavaScript
- Title
- adding border in react native
- Category
- JavaScript
- Title
- event listener in react
- Category
- JavaScript
- Title
- console log larger
- Category
- JavaScript
- Title
- how to get duplicate values from array in javascript
- Category
- JavaScript
- Title
- append after element jquery
- Category
- JavaScript
- Title
- get number of creeps screeps
- Category
- JavaScript
- Title
- iterate over array javascript
- Category
- JavaScript
- Title
- axios set authorization header
- Category
- JavaScript
- Title
- ajax file upload jquery
- Category
- JavaScript
- Title
- datetime to date javascript
- Category
- JavaScript
- Title
- datatable on error.dt
- Category
- JavaScript
- Title
- iterate array in javascrpt
- Category
- JavaScript
- Title
- if else practice javascript
- Category
- JavaScript
- Title
- how to delete object property of array javascript
- Category
- JavaScript
- Title
- install vue-cli
- Category
- JavaScript
- Title
- javascript array push
- Category
- JavaScript
- Title
- How to make string shorter javascript
- Category
- JavaScript
- Title
- javascript check if date is today
- Category
- JavaScript
- Title
- How can I know which radio button is selected via jQuery
- Category
- JavaScript
- Title
- colors in node js console
- Category
- JavaScript
- Title
- how to use if else inside jsx in react
- Category
- JavaScript
- Title
- get iso date javascript
- Category
- JavaScript
- Title
- express json body
- Category
- JavaScript
- Title
- callback function js
- Category
- JavaScript
- Title
- get text of selected option jquery
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- how to limit input type max length
- Category
- JavaScript
- Title
- collision detection three js
- Category
- JavaScript
- Title
- integers to space separated string in javascript
- Category
- JavaScript
- Title
- difference between == and === in javascript
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- javascript add to array
- Category
- JavaScript
- Title
- function and returns node js
- Category
- JavaScript
- Title
- bracket notation javascript
- Category
- JavaScript
- Title
- how to find unique elements in array in javascript
- Category
- JavaScript
- Title
- date add hours javascript
- Category
- JavaScript
- Title
- javascript array contains
- Category
- JavaScript
- Title
- .push js
- Category
- JavaScript
- Title
- how to check if window size of browser s changed javascript
- Category
- JavaScript
- Title
- does a function inside a function need to be called?
- Category
- JavaScript
- Title
- create an element jquery
- Category
- JavaScript