问题描述
我对反应导航还是个新手.我想在登录成功后重置路线信息.但是,有一些参数,例如我希望传递到主页的登录信息.
I am still new to react navigation.I wish to reset the route information after login successfully. However there is some parameters like login information I wish to pass to the main page.
import { NavigationActions } from 'react-navigation'
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Profile',
params:{user:this.state.username}})
]
})
this.props.navigation.dispatch(resetAction)
在个人资料页面中,我想访问参数,所以我使用这个:
In the Profile page, I want to get access to the params, so I use this:
constructor(props){
super(props)
this.state = { username: this.props.navigation.state.params.user,
}
但我收到:undefined 不是控制台日志中的对象(正在评估_this.props.navigation.state.params.user").我可以知道我应该如何获取参数吗?谢谢
but I received: undefined is not an object (evaluating '_this.props.navigation.state.params.user') in the Console log.May I know how should I get the parameters? Thank you
推荐答案
您可以像这样在 react-native stack navigator 的重置操作中传递参数:-
You can pass the params in reset action of react-native stack navigator like this:-
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'ScreenName', // name of the screen you want to navigate
params: {
paramsKey: paramsValue // this second parameter is for sending the params
}
})
],
});
this.props.navigation.dispatch(resetAction);
并从导航状态参数中获取第二个屏幕中的参数值,如下所示:-
And get the parameter value in second screen from the navigation state params like this:-
static navigationOptions = ({navigation, screenProps}) => {
const params = navigation.state.params || {}; // You can get the params here as navigation.state.params.paramsKey
console.log("CompareScreen product_id:", params.paramsKey);
}
这篇关于React Navigation Reset 可以加参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!