3 ms·
I've always much preferred using local variables to hold conditional fragments. Since React will safely ignore null/undefined, you can do: let child; if (s
by nkohari 5y ago
I've always much preferred using local variables to hold conditional fragments. Since React will safely ignore null/undefined, you can do:
let child;
if (someCondition) {
child = <SomeComponent ... />;
}
return (
<div>
Maybe here's a child: {child}
</div>
);
This lets you avoid embedding conditional logic in your (already pretty dense) JSX tree.
- postalrat 5y agoI do this sometimes but having to look up where child was defined and everything that could have changed it can be tiresome. That's why a function can be better, at least you know that the value isn't changed somewhere unexpected.