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,
}
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
You are passing parameters by using
username
as key. So when you are retrieving it your code should look like thisStep 1: Pass params
Step2: Get Params
You can pass the params in reset action of react-native stack navigator like this:-
And get the parameter value in second screen from the navigation state params like this:-
This works for me:
NavigationActions.navigate({ routeName: 'Location', params: { username: 'Luchi' }}),
const params = this.props.navigation.state.params || {}; if (!(params && params.username)) { alert('hi '+params.username) }