javascript - How can I remove a component using the children prop in React -


i trying achieve scenario:

  1. i have foo component bar prop

  2. if bar prop true --> mount bar component inside foo component

  3. if bar prop false --> unmount bar component
  4. i don't want example component aware of show/hide, want foo know when show or hide it
  5. 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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -