您可以从“第一个”组件中执行此操作。您必须使用事件发射器从 renderRight 按钮发出单击事件,在您的 First 组件中捕获该事件并从那里推送新路由。您也可以使用它传递道具。这是解决方案。
首先定义 renderRight 按钮类。
var EventEmitter = require('EventEmitter');
class NextButton extends React.Component {
constructor(props) {
super(props);
this.nextPressed = this.nextPressed.bind(this);
this.getView = this.getView.bind(this);
}
render() {
return(
this.getView()
);
}
getView() {
return (
<TouchableHighlight
onPress={this.nextPressed}
underlayColor='transparent'
style={Styles.rightBarButton}>
<Text
style={[Styles.rightBarButtonText, Styles.fontStyle, {color:'#6A6A6A'}]}>Next</Text>
</TouchableHighlight>
);
}
nextPressed() {
console.log("nextPressed CALLED!");
this.props.emitter.emit('nextpressed');
}
}
然后将其用作“第一个组件”内的 renderRight 按钮。此外,您需要捕获触发的事件并推送下一个组件。
class First extends Component {
static route = {
navigationBar: {
title: 'FIRST SCREEN',
titleStyle: [Styles.navigationBarTitle, Styles.fontStyle],
tintColor: '#000',
renderRight: ({ config: {eventEmitter} }) => (<NextButton emitter={eventEmitter}/>)
},
}
constructor(props) {
super(props);
this.pressedEventCallback = this.pressedEventCallback.bind(this);
}
...
componentWillMount() {
this._buttonPressSubscription = this.props.route.getEventEmitter().addListener('nextpressed', this.pressedEventCallback);
}
componentWillUnmount() {
this._buttonPressSubscription.remove();
}
...
pressedEventCallback() {
this.props.navigator.push(Router.getRoute('Second', {prop1: prop1, prop2: prop2}));
}
...
}
这样,您可以从按钮触发事件,然后在您的组件中捕获,然后导航到传递道具的其他屏幕或做任何您能做的事情。让我知道它是否适合你。