Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation Reset Can add parameters?

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

like image 632
cg1991 Avatar asked Jun 09 '17 03:06

cg1991


People also ask

How do you reset params in react navigation?

I was able to clear the params by manually creating a function and setting the params that are passed to null. The clearParams function is called when the header button is pressed. That doesn't exactly work on web, on the other hand setting them to undefined instead does work.

What does reset method of Stackaction do?

reset​ The reset action wipes the whole navigation state and replaces it with the result of several actions. index - number - required - Index of the active route on routes array in navigation state .

Can we pass params in goBack react navigation?

Unfortunately, you can not pass data like this when using the goBack() method.


4 Answers

Yes it can. React Navigation 5+ let's us easily use the navigation prop reset the routes history, then it allows us to pass a params property into the targeted route object.

navigation.reset({
    index: 0,
    routes: [{name: 'DriveInfosAdded', params: {data}}],
  });
like image 156
Guillaume250 Avatar answered Oct 12 '22 01:10

Guillaume250


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);
}
like image 38
Vishal Dhaduk Avatar answered Oct 12 '22 02:10

Vishal Dhaduk


This is working for me on React Navigation 5:

To reset the state of navigation, navigate to the screen 'OrderDetails' and pass the parameter 'orderCode' to this screen:

navigation.reset({index: 0, routes: [{ name: 'OrderDetails', params: { orderCode: 'whatever' } }] })

To retrieve the parameter orderCode in the new route 'OrderDetails':

function OrderDetails({theme, route, navigation}) {
const { orderCode } = route.params;
...
}
like image 44
S. Funmajer Avatar answered Oct 12 '22 02:10

S. Funmajer


You are passing parameters by using username as key. So when you are retrieving it your code should look like this

Step 1: Pass params

this.props.navigation.navigate('Your-Screen_name',{ username: 'Lucy' })

Step2: Get Params

this.state = { 
    username: this.props.navigation.state.params.username, 
}
like image 2
Kishan Vaghela Avatar answered Oct 12 '22 01:10

Kishan Vaghela