Sub topic 12
Rendering nothing with null
This chapter assumes you know function components, props, JSX, and the useState hook. It covers how a component renders nothing, and the mistakes that come with it.
What null does in a component
A component must return something React can render. Sometimes the right output is nothing at all: a banner with no message, a badge with a count of zero, a modal that is closed. In those cases, return null.
function Banner({ message }) {
if (!message) {
return null;
}
return <div className="banner">{message}</div>;
}When message is empty, Banner adds nothing to the page. The parent still renders <Banner /> as usual. It does not need to know whether the child will show anything.
Values React skips
React ignores a few values when they appear as children. They produce no DOM output:
nullundefinedtrueandfalse
The empty string "" also renders nothing visible. The number 0 is different. React treats it as a real value and prints it, which is the pitfall from the previous chapter.
function Demo() {
return (
<div>
{null}
{undefined}
{false}
{true}
<p>Only this paragraph appears.</p>
</div>
);
}Returning undefined by mistake
A component function that returns undefined is a common error. It usually happens when you forget the return keyword or put the JSX on a new line after it.
function Broken({ show }) {
if (show) {
<p>Hello</p>; // no return, so the function ends with undefined
}
}Modern React accepts undefined from a component, but it is almost always a bug. Return null explicitly when you mean "nothing". That makes your intent clear to the next reader.
null in the middle of JSX
You can use null as one branch of a ternary. It reads well when the "off" case is empty.
function Status({ error }) {
return <div>{error ? <p role="alert">{error}</p> : null}</div>;
}For a simple show-or-hide, error && <p>...</p> does the same thing. Use the ternary with null when the condition could be a number or an empty string, because the explicit null avoids printing a falsy value.
Returning null does not preserve state
When a component returns null, it is still mounted. Its state and effects stay alive. When a parent stops rendering the component altogether, React unmounts it and discards its state.
function Parent({ open }) {
return (
<>
{/* Panel stays mounted and keeps its state, but may show nothing */}
<Panel open={open} />
{/* Panel is unmounted when open is false, and its state is lost */}
{open && <Panel open />}
</>
);
}Pick based on whether you need the state to survive. If a form inside a hidden panel must keep what the user typed, let the component return null itself, or keep it mounted and hide it. If a fresh start is fine, conditionally render it from the parent.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Forgetting `return` | Component returns `undefined` | Add `return null` or the JSX |
| Writing `return ""` for nothing | Works, but unclear | Use `return null` |
| Expecting hidden state to reset | State persists when a child returns `null` | Render conditionally in the parent, or use a `key` |
| Using `count && <X />` | Prints `0` | Use `count > 0 && <X />` |
Exercise
A Tooltip component holds an isPinned state. Its parent writes {show && <Tooltip />}. A user pins the tooltip, then show becomes false and later true again. Is the tooltip still pinned? What would change if the parent always rendered <Tooltip show={show} /> and the component returned null when show was false?
Show answer
No. When show is false, the parent stops rendering Tooltip, so React unmounts it and discards isPinned. When show becomes true again, a new instance starts with the initial state. If the parent always renders <Tooltip show={show} /> and the component returns null, the component stays mounted, so isPinned is kept and the tooltip is still pinned when it reappears.