在NavigatorIOS – React Native中调用onRightButtonPress的函数

我在反应原生NavigatorIOS中使用onRightButton。 我希望能够调用一个驻留在我所推的组件中的函数,但是我不知道如何实现这个function。 这是一个代码示例:

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

我怎样才能做到这一点?

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

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

以下将起作用:

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