bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Conditional Rendering

In React, you can conditionally render components.

There are several ways to do this.

if Statement

We can use the if JavaScript operator to decide which component to render.

We'll use these two components

function MissedGoal() {
 return <h1>MISSED!</h1>;
}
function MadeGoal() {
 return <h1>Goal!</h1>;
}

Example

Now, we'll create another component that chooses which component to render based on a condition:

function Goal(props) {
 const isGoal = props.isGoal;
 if (isGoal) {
 return <MadeGoal/>;
 }
 return <MissedGoal/>;
}
createRoot(document.getElementById('root')).render( <Goal isGoal={false} /> );

Try changing the isGoal attribute to true :

Example

createRoot(document.getElementById('root')).render( <Goal isGoal={true} /> );

Logical && Operator

Another way to conditionally render a React component is by using the && operator.

In the example below, the heading will only be rendered if the props.brand property is not empty:

Example

The right side of && will only be rendered if the left side is true:

function Car(props) {
 return ( <> {props.brand && <h1>My car is a {props.brand}!</h1>}
 </> );
}
createRoot(document.getElementById('root')).render( <Car brand="Ford" /> );

If props.brand evaluates to true, the expression after && will render.

Example

createRoot(document.getElementById('root')).render( <Car /> );

Ternary Operator

Another way to conditionally render elements is by using a ternary operator.

condition ? true : false

We will go back to the goal example.

Example

Return the MadeGoal component if isGoal is true , otherwise return the MissedGoal component:

function Goal(props) {
 const isGoal = props.isGoal;
 return ( <> { isGoal ? <MadeGoal/> : <MissedGoal/> }
 </> );
}
createRoot(document.getElementById('root')).render( <Goal isGoal={false} /> );

To learn more, see the ternary operator section.

Previous

React Events

Next

React Lists

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

if StatementLogical && OperatorTernary Operator