Skip to content
Advertisement

How do you pass state variables between two separate functional components in React Native?

JavaScript

}

JavaScript

I don’t know react native that well and I need help with the code writing itself. I tried using useContext() but it was written in react and I’m a beginner so I don’t really understand how to implement it. Also, there are no parent functions for this piece of code and both functions are in the same file. I need someway to first pass the state variables from the “passVar” function into the “recieveVar” function. I made a parent function called “parent” that encompasses both functions and declared the state variables inside the parent function but there was a problem because Stack Navigator said that it couldn’t find the recieveVar and passVar function since they were child components

Advertisement

Answer

You can pass state variables between two functional components using props. You can declare state variable in your PassVar like:

JavaScript

and then pass these to RecieveVar() by rendering receivevar inside your Passvar function and then passing these values as params like:

JavaScript

then you can receive these values inside RecieveVar() like:

JavaScript

If you are not rendering RecieveVar inside PassVar then you can send these values to receiveVar using navigation. Like:

JavaScript

then you can receive these values inside receiveVar() as :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement