Home > other >  Passing data from one screen to another and to another screen again in React native
Passing data from one screen to another and to another screen again in React native

Time:10-18

When routing data using flatlist from one screen to another, I was able to do that using route.param. But can I send the data from screen one to screen two to screen two to screen three?

What method should I use?

CodePudding user response:

you shoudl check about the navigation :

onPress={() => {
    navigation.navigate('MyScreen', {
        myData: data,
    });
}}

don't forget const MyCurrentScreen = ({navigation}) => {

and then in the 'MyScreen' where you navigate, you will have access to

const MyScreen = ({route}) => {
  const data = route.params.myData;

CodePudding user response:

It depends on the flow of navigation of screens. like if you are passing data from first screen->second screen->third screen like this then you can pass data on navigation params.

but it will gives you param error if you navigate to third screen from another screen except second screen so in that you can use React-redux.

on your screen file import useNavigation. I prefer this to import

 import {useNavigation} from '@react-navigation/native;

just before return use like this.

  const navigation = useNavigation();
 

from first screen onPress button use this just you screen name with this name data as you data

onPress ={()=> {
  navigation.navigate('secondScreen',{
  myData:paramsData})}};

on Second Screen get your value like this and do this same for third screen. just pass "data" in navigation params of second screen.

 const secondScreen =({props}) => {
  let data = props.route?.params?.myData
 }

CodePudding user response:

The more better approach would be using redux or context hooks

  • Related