反应原生路由器通量自定义导航栏访问方法组件

时间:2017-11-29 12:25:49

标签: reactjs react-native react-native-router-flux

我有一个带自定义导航栏的场景

                <Scene
                      navBar={CustomNavBar}
                      component={MComponent}                   

                  />

MyCOmponent拥有名为_hello()

的方法
 export default class MComponentextends PureComponent {
      constructor(props) {
         super(props);     
     }
   _hello = () => {
    console.warn('HELLO WORLD')
}

是否可以在导航栏组件中调用MyComponent _hello()方法?

export default class CustomNavBar extends Component {
constructor(props) {
    super(props);
    this.state = {        
    };
    this.props._hello () // does not work
    }
}

2 个答案:

答案 0 :(得分:0)

使用Scene作为中介。

首先,在Scene

中为CustomNavBar添加一个回调道具
<CustomNavBar cb={this._callback}>

然后为MyComponent设置ref,也在Scene

<MyComponent ref={(c)=>this._myComponent=c}>

如果要在CustomNavBar中调用_hello,请调用cb

this.props.cb();
将调用

和_callback在Scene中,使用this._myComponent来调用_hello

this._myComponent._hello()

答案 1 :(得分:0)

 <Scene 
navBar={CustomNavBar cb={this._callback}} 
component={MComponentef={(c)=>this._myComponent=c}}
/> 

将props传递给Scene CustomNavBar throw error =&gt;检查cardstack的方法