Javascript React Native—导航期间传递给组件的参数在以后再次导航到该组件时保留在该组件中
作为导航设置的一种变通方法,我最终不得不先导航到子导航器,然后再导航到该导航器上的路线(我从父导航器转到子导航器)。最初加载子导航器时,它会加载Javascript React Native—导航期间传递给组件的参数在以后再次导航到该组件时保留在该组件中,javascript,react-native,parameters,parameter-passing,listener,Javascript,React Native,Parameters,Parameter Passing,Listener,作为导航设置的一种变通方法,我最终不得不先导航到子导航器,然后再导航到该导航器上的路线(我从父导航器转到子导航器)。最初加载子导航器时,它会加载CameraScreen.js,但我需要进入RentableScreen.js,这是子导航器上带有CameraScreen.js的一个屏幕。我使用CameraScreen.js从主App导航器(HomeScreen.js)进入RentableScreen.js)。我这样做的方式是使用addListener来监听页面何时被关注,它会得到参数,告诉它继续Re
CameraScreen.js
,但我需要进入RentableScreen.js
,这是子导航器上带有CameraScreen.js
的一个屏幕。我使用CameraScreen.js
从主App
导航器(HomeScreen.js
)进入RentableScreen.js
)。我这样做的方式是使用addListener
来监听页面何时被关注,它会得到参数,告诉它继续RentableScreen.js
,然后导航到RentableScreen.js
——代码如下:
CameraScreen.js
这一切都正常工作,但我传递的参数似乎“卡在”
CameraScreen.js
,因为下次我尝试导航到它时(通过按下TabNavigator
上的选项卡),它的行为就像我发送了它以前收到的参数一样,但我没有。似乎参数正在保存/保留在某个位置-如何清除它?或者如何使addListener
在从选项卡导航器来时不引起注意?此代码按我所希望的方式工作:
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
}
}
);
}
也适用于此.props.navigation.DangerlyGetParent().state.params=null;谢谢你,伙计!
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
}
}
);
}