问题
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