React - how to determine if a specific child component exists?
问题 If I have this structure: const MyComponent = (props) => { return ( <Wrapper />{props.children}</Wrapper> ); } and I use it like this: <MyComponent> <SomeInnerComponent /> </MyComponent> How can I check to see if <SomeInnerComponent /> has specifically been included between <MyComponent></MyComponent> , from within the MyComponent function? 回答1: Given that you want to check that SomeInnerComponent is present as a child or not, you could do the following const MyComponent = (props) => { for