ALCode
ALCode

Reputation: 21

Passing state through navigation parameter react native

Ive got the state: const [isPressed, setIsPressed] = useState(false) Then I want to pass the setIsPressed when navigating to another sreen, which i wrote: On the from Screen: onPress{() => navigation.navigate('Map', {setIsPressed})} On the to Screen: onPress={() => {navigation.navigate('DM'); setIsPressed(true);}}

However, I get an error saying 'setIsPressed is not a function' when pressing on the To Screen button

How do I fix this?

Upvotes: 0

Views: 647

Answers (1)

Alija Fajic
Alija Fajic

Reputation: 598

This is the way you can do it, here is you first screen:

const togglePressed = () => setIsPressed(prev=>!prev);



onPress={() =>
   navigation.navigate('ScreenName', {
     onRefreshParent: () => togglePressed(),
   })
}

And in you other screen use it like this:

 onPress={() => route.params.onRefreshParent && route.params.onRefreshParent()}

Upvotes: 0

Related Questions