Say I'm making a wrapper component which should only render itself if some child nodes are passed in:
const Wrapper = ({children}) => {
if (!children) return null
return <div>{children}</div>
}
The problem is that children
could be a Fragment
containing null, or an array of nulls. Or a fragment containing a fragment containing...
<Wrapper>hey</Wrapper> // renders <div>hey</div>
<Wrapper>{null}</Wrapper> // renders nothing
<Wrapper>{[null]}</Wrapper> // renders <div/>
<Wrapper><React.Fragment>{null}</React.Fragment></Wrapper> // renders <div/>
Is there a canonical way to check for all these conditions?