Showing the right thing at the right time
React apps constantly need to show different UI based on conditions: a spinner while loading, an error message if something failed, a login button if the user isn’t authenticated. This is conditional rendering. You already know the JavaScript for this — ternary operators,&&, and if/else from the conditionals lesson. In React, you use the same patterns inside JSX.
&& — show something or nothing
The most common pattern. Show an element if a condition is true, nothing if false:&& is perfect when you want to either show something or show nothing. If the left side is truthy, the right side renders. If falsy, nothing renders.
Ternary — show one thing or another
When you need to choose between two different outputs:Early returns — the cleanest pattern
For loading, error, and empty states, use early returns at the top of your component:The loading / error / data pattern
You’ll write this in almost every component that fetches data:Conditional CSS classes
Conditional rendering with variables
For more complex conditions, compute the JSX before the return:Preventing rendering with null
Returnnull to render nothing:
What’s next?
You can show and hide UI based on conditions. Now let’s learn how to render lists of data — the most common thing you’ll do with API responses.List rendering
Render arrays of data as lists of components