【问题标题】:Function to call onRightButtonPress in NavigatorIOS - React Native在 NavigatorIOS 中调用 onRightButtonPress 的函数 - React Native
【发布时间】:2015-08-07 03:28:59
【问题描述】:

我在 react-native NavigatorIOS 中使用 onRightButton。 我希望能够调用驻留在我正在推送的组件中的函数,但我不知道如何实现。 这是代码示例:

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

我该怎么做?

【问题讨论】:

    标签: javascript ios function react-native navigator


    【解决方案1】:

    SingleResultView 尚未实例化,因此您无法访问其方法。

    以下方法可行:

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

    【讨论】:

      【解决方案2】:

      ref 回调道具是你的朋友! 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;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-26
        • 2017-09-04
        • 1970-01-01
        相关资源
        最近更新 更多