Function to call onRightButtonPress in NavigatorIOS - React Native

落花浮王杯 提交于 2019-12-13 13:25:42

问题


I'm using the onRightButton in a react-native NavigatorIOS. I'd like to be able to call a function which resides into the component I'm pushing, but I can't get how to achieve that. This is an example of code:

this.props.navigator.push({
  component: SingleResultView,
  title: "SomeTitle",
  rightButtonIcon: require('image!mapsmarker'),
  passProps: {
    userPosition: this.props.userPosition
  },
  onRightButtonPress: () => { SingleResultView.foo(); } // NOT WORKING!
});

How can I do that?


回答1:


The ref callback prop is your friend! https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute

  goToResults() {
    this.props.navigator.push({
      component: SingleResultView,
      title: "SomeTitle",
      rightButtonIcon: require('image!mapsmarker'),
      passProps: {
        userPosition: this.props.userPosition,
        ref: this.onResultsRef,
      },
      onRightButtonPress: () => { this._resultsView && this._resultsView.foo(); }
    });
  }

  onResultsRef(resultsViewRef) {
    this._resultsView = resultsViewRef;
  }



回答2:


SingleResultView hasn't instantiated yet, so you have no access to its methods.

The following would work:

this.props.navigator.push({
  onRightButtonPress: SingleResultView.prototype.foo
});


来源:https://stackoverflow.com/questions/30441908/function-to-call-onrightbuttonpress-in-navigatorios-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!