如果我尝试使用Object.keys(children).length并且只有 1 个子组件,那么它会给我一个子组件所具有的键的数量。这种方法只有在有多个子组件时才有效。我如何找到一个组件的子组件数量?
<Navbar>
<Component 1/>
</Navbar>
const Navbar = ({ children }) => {
console.log(Object.keys(children).length); //displays properly only if more than 1 child component
console.log(children.length); //undefined
...
Cats萌萌
相关分类