Friday 16 October 2020

React component children detect if empty / null before render

"react": "16.8.4"

Hello, someone knows how to check if there exists children in a (functional) component (before render them)

React.Children.count
React.Children.toArray(children)

wont work

the children is $$typeof: Symbol(react.element)

the code example is

function ContextMenuItems(): JSX.Element | null {
   if (no-items) return null;  
   ...
}

class ContextMenu extends React.Component {
   public render(): JSX.Element | null {
      if (this.props.children === null) { //ContextMenuItems empty check
        return null;
       }
       return <ContextMenu>{this.props.children}</ContextMenu>
   }
}

For any help, idea thankful



from React component children detect if empty / null before render

No comments:

Post a Comment