Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data to the parent page using React Native Navigation (v5)?

I'm using https://reactnavigation.org/ (version 5.0.1) in my project with GraphQL Apollo client.

I have a page with a form where the user needs to select some options from a list.
In the first page, I have a button with this code:

props.navigation.navigate('SelectTagsPage', {
            onSelect: (selectedIds) => {
              // update the form state
              setTagsIds(selectedIds)
            },
          });

On the tags page I have this:

const { onSelect } = props.route.params;

//...

<Button onPress={() => { onSelect(ids) }}>

So, basically I'm passing a function when calling the navigation.navigate, and I'm executing this function to send data back to the initial screen.

This is working very well, however when I open the TagsPage I'm getting this warning:

We found non-serializable values in the navigation state, which can break usage such as persisting and restoring state. This might happen if you passed non-serializable values such as function, class instances etc. in params

If passing a function in the params is a problem, what is the best way to achieve the same functionality of sending data from a page to the parent page and solve this warning message?

like image 579
Victor Avatar asked Feb 21 '20 21:02

Victor


People also ask

How do you pass data from child to parent in react navigation?

To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

How do you send data in navigation goBack in React Native?

Unfortunately, you can not pass data like this when using the goBack() method. One workaround is to pass a callback function to the screen you are navigating to. Then, you can call that function with whatever data you wanted to pass back before calling the goBack() method.


1 Answers

You can pass them as params:

navigation.navigate('NameOfPreviousScreen', { selectedIds });

When you navigate to a previous screen, it acts like goBack, but also passes any params you want.

https://reactnavigation.org/docs/en/params.html

like image 93
satya164 Avatar answered Sep 18 '22 22:09

satya164