React - how to determine if a specific child component exists?

前端 未结 2 1139
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 19:09

If I have this structure:

const MyComponent = (props) => {
    return (
        {props.children}
    );
}

a

2条回答
  •  日久生厌
    2021-02-10 19:22

    Just want to provide an answer for a similar but different need, where you might have an HOC wrapping several layers of components, and you'd like to see if the HOC has already wrapped a component. The method I came up with was to have the HOC add a data-attribute onto the component to serve as a flag, which the HOC could then check on subsequent runs.

    const WithFoo = Component = props => {
      return props["data-with-foo"]
        ? 
        : (
          
            
          
        );
    };
    

提交回复
热议问题