javascript
Render item condition based React
We use the if else condition statement to implement something condition-based. In React you can render HTML components based on different condition using below code.
function template() {
const renderItem = true;
return <div>
{renderItem &&
<div>This will render if renderItem is true</div>
}
</div>
}
You can render items and HTML in React based on condition. In the code snippet, we are rendering div id 'renderItem' variable is true. It will not render on the page if we set this variable false.
Multiple conditions
function template() {
const child = 10;
const message = "hello";
return <div>
{child > 5 && message === "hello" &&
<div>This will render if renderItem is true</div>
}
</div>
}
The above code will render the <div> element if child is greater than 5 and message is equals to hello.
Was this helpful?
Similar Posts
- Lodash - Get index of array item based on some condition
- Render a string as HTML in React
- Test every item of array against some condition in javascript
- Highlight the highest value list item using React
- Remove items starting from last untill given condition is false Lodash
- Remove items begin from start until given condition is false Lodash
- Array.find() - get the very first element satisfy a condition Javascript