✨ Conditional rendering in React with finesse
Learn how to make your own component to handle conditional rendering in React.

Search for a command to run...
Learn how to make your own component to handle conditional rendering in React.

No comments yet. Be the first to comment.
Brief introductions Hi, my name is Brett Thurston. I'm a developer and maker who enjoys working in the front-end spectrum of development and design. I've made an app and wanted to recap some highlights and lessons learned from my journey so far. I'd ...

An introduction to some much needed medicine

There may be times when you don't want to trigger renders when capturing data from the user. useState, by now, is a well known and handy hook since it was implemented in React 16.8. When setting our state variable with useState, it causes a render of...

In this article you'll find a method to use whenever you have a long string that may funk up your UI components. I was working on a list item component for a project and found a problem we run into a lot. What I have is a list of cards that bring in...

So if you've spent a measure of time with React you'll come across the scenario of needing to render an element based on a condition.
For example, what if you had a component that counted something. And you wanted to show the actual count in a component. However, if it ever dropped below 1, you might want show a message in its place. Informing the user something or presenting a new call to action to do something else.
You could write it in a ternary operator like this:
import { useState } from "react";
import RenderIf from "components/RenderIf";
export const Sample = () => {
const [count, setCount] = useState(2);
const increment = () => {
let newCount = count;
setCount((newCount += 1));
};
const decrement = () => {
if (count === 0) return;
let newCount = count;
setCount((newCount -= 1));
};
return (
<>
<button onClick={increment}>Add</button>
<button onClick={decrement}>Subtract</button>
{count > 0
? <p>I have {count}, and that's pretty cool.</p>
: <p>Sorry, I'm all out.</p>
}
</>
);
};
This works OK, but in my opinion doesn't read as easy as another option. What could a component look like to handle this use case?
We can build a component that renders the content it wraps based on a condition we feed it. If that condition isn't true then we'll render something else. This will handle this use case with a little more finesse, IMO. 😎
First let's make a component called RenderIf. Below are the JSX and TypeScript version. Pick your poison.
JSX:
import { ReactNode } from "react";
const RenderIf = ({ children, isTrue, fallback }) => {
return isTrue ? children : fallback;
};
export default RenderIf;
TypeScript:
import { ReactNode } from "react";
type Props = {
children: ReactNode;
isTrue: boolean;
fallback?: any;
};
const RenderIf = ({ children, isTrue, fallback }: Props) => {
return isTrue ? children : fallback;
};
export default RenderIf;
This component that we've made has 3 props being passed in:
We destructure those props and pass them into the component. Children is whatever element this component is wrapping. Then we pass the condition of when to render the wrapped element with the isTrue prop.
Whenever this condition is true (or truthy) it will render the wrapped element. If the condition is NOT true, then it renders whatever we pass in the fallback argument.
In TypeScript I've set this to be an optional argumentin the type of Props. Why? I may not want to always pass a fallback element. So if I pass no fallback argument prop then will return undefined and a blank component will render.
In the JSX version of , this happens naturally.
So your fallback argument can be a message saying, 'Hey you're out of counts' or it could be a button to buy more counts. You get the idea.
import { useState } from "react";
import RenderIf from "components/RenderIf";
export const Sample = () => {
const [count, setCount] = useState(2);
const increment = () => {
let newCount = count;
setCount((newCount += 1));
};
const decrement = () => {
if (count === 0) return;
let newCount = count;
setCount((newCount -= 1));
};
return (
<>
<button onClick={increment}>Add</button>
<button onClick={decrement}>Subtract</button>
<RenderIf isTrue={count > 0} fallback={<p>Sorry, I'm all out.</p>}>
<p>I have {count}, and that's pretty cool.</p>
</RenderIf>
</>
);
};
So now you've got a handy component you can use over and over again to render conditional elements in React. Maybe you want to extend this component to do other things too. What else could you see it do? Let me know what you think and if you'd take a different approach.
Good luck and happy building. 🔨