React Native Child Parent communication

后端 未结 2 2222
庸人自扰
庸人自扰 2021-02-19 18:16

Parent Child communication seems no problem using pass props:

mainpage.ios.js code:

var OtherPage = require(\'./otherpage\');


        
2条回答
  •  长发绾君心
    2021-02-19 18:57

    I highly recommend that you use Flux here. When you find yourself in need of various components needing the same information, then it means you need Flux. You can of course use a bunch of callbacks and so on, but as your app grows and becomes more complex, it will be much more difficult to manage it without something like flux.

    With flux you will have your mainpage component listen to changes and once the change took place do something. For example:

    mainpage:
    
    componentDidMount() {
      SomeStore.addChangeListener(this.updateComponent)
    }
    
    componentWillUnmount() {
      SomeStore.removeChangeListener(this.updateComponent)
    }
    
    updateComponent() {
      this.setState value: SomeStore.getValue()
    }
    
    
    
    otherpage:
    
    toggle() {
      SomeActions.change(this.props.value + 1)
    }
    
    render() {
      
        Some View
      
    }
    

提交回复
热议问题