DEV Community

Discussion on: React Clean Code - Simple ways to write better and cleaner code

Collapse
 
garystorey profile image
Gary Storey

For those looking for a short simple solution to conditional rendering:

const Show = ({when=false, children}) = when ? <>{children}</> : null
Enter fullscreen mode Exit fullscreen mode

and usage in your component:

const [show, setShow] = useState(false);
<Show when={show}>
Hello!
</Show>
Enter fullscreen mode Exit fullscreen mode

It uses a Fragment as a wrapper in case the user does not have a single parent element in children