【发布时间】:2019-03-22 15:52:58
【问题描述】:
“反应”:“16.8.4”
您好,有人知道如何检查(功能)组件中是否存在子组件(在渲染它们之前)
React.Children.count
React.Children.toArray(children)
不会工作
孩子们是$$typeof: Symbol(react.element)
代码示例是
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>
}
}
如有任何帮助,感谢您的想法
【问题讨论】:
-
在那个问题中检查的是特定的孩子,这里我想检查孩子的数量
-
@James 除了对您添加的赏金的赞成答案之外,您还期待什么?您的问题仍然是旧版本的 react 吗?
-
嗨,@user11243583,看看我的回答,我想这就是你要找的
-
一开始我很难理解这里投票最高的答案。直到一分钱掉下来,我才明白
children.type()真的只是试图调用一个孩子的渲染函数。我写了一个 blog article 涵盖了这个用例,希望在解释事情方面做得更好。希望对您有所帮助。
标签: javascript reactjs