Short Circuit Evaluation
So instead of writing your JSX like below:
function SampleComponent({ isTrue }) { return isTrue ? <p>True!</p> : null; };
function SampleComponent({ isTrue }) { return isTrue && <p>True!</p>; };
Simple Early Returns
If you are dealing with complex conditions, it's recommended to use simple early returns where each condition automatically returns the necessary value.
function SampleComponent({ a, b, c, d, e }) { const condition = a && b && !c; if (!condition) return <p>Foo</p>; if (d) return <p>Bar</p>; if (e) return <p>Baz</p>; return <p>Qux</p> }
Leave a reply