作为导航设置的解决方法,我最终必须先导航到子导航器,然后再导航到该导航器上的路线(我从父导航器转到子导航器)。最初加载子导航器时,它会加载 CameraScreen.js
,但我需要使用 CameraScreen 访问
。我使用 RentableScreen.js
这是子导航器上的屏幕.jsCameraScreen.js
从主 App
导航器 (HomeScreen.js
) 访问 RentableScreen.js
。我这样做的方法是使用 addListener
监听页面何时聚焦,并获取参数,告诉它继续 RentableScreen.js
然后导航到 RentableScreen.js
- 代码如下:
CameraScreen.js
...
componentDidMount() {
this._sub = this.props.navigation.addListener(
'didFocus',
() => {
if(this.props.navigation.getParam('param', '') === 'RentTab') {
console.log('I HEARD YOU');
this.navigator && this.navigator.dispatch(
NavigationActions.navigate({
routeName: 'Rentable',
})
);
}
}
);
}
...
这一切都工作正常,但我传递的参数似乎“卡在”CameraScreen.js
上,因为下次我尝试导航到它时(通过按 >TabNavigator
)它的行为就好像我向它发送了它之前收到的参数,但我没有。参数似乎被保存/保存在某处 - 我该如何清除它?或者如何使 addListener
在来自 TabNavigator
时不引起注意?
最佳答案
这段代码按照我想要的方式工作:
componentDidMount() {
this._sub = this.props.navigation.addListener(
'didFocus',
() => {
if(this.props.navigation.getParam('param', '') === 'RentTab') {
this.navigator && this.navigator.dispatch(
NavigationActions.navigate({
routeName: 'Rentable',
})
);
this.props.navigation.state.params = null; //THIS CLEARS THE STATE PARAMS HOW I WAS DESCRIBING
}
}
);
}
关于javascript - React Native - 在导航期间传递给组件的参数在稍后再次导航到该组件时保留在该组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52324288/