javascript - How can I remove a component using the children prop in React -
i trying achieve scenario:
i have foo component bar prop
if bar prop true --> mount bar component inside foo component
- if bar prop false --> unmount bar component
- i don't want example component aware of show/hide, want foo know when show or hide it
- bar not necessary direct child of foo, can nested deeper in children of foo
const example = () => { return ( <foo bar={true/false}> <div>some div</div> <bar></bar> </foo> ) }; class foo extends react.component { componentwillreceiveprops({bar}) { if (bar) { /* want show bar!! */ } else { /* want remove bar!! */ /* maybe doing like: this.props.children.searchforbar().removenode() */ } } render () { return ( <div>{this.props.children}</div> ) } }; const bar = () => 'i bar';
i've tried manipulate this.props.children react blocked me modifying children myself.
do need maybe outer service communication between these 2 components ?
should use context maybe ?
looking advice on how approach issue.
one of possible solutions use kind of message bus or pubsub. in kind of approach, bar component have subscribe bus, , show/hide itself. foo component publish appriopriate messages in componentwillreceiveprops. more: here
Comments
Post a Comment