const Screen1 = () => {
const navigation = useNavigation()
const onPress = () => {
navigation.navigate("Screen2", {
thing1:"my thing",
thing2:"my second thing"
})
}
}
const Screen2 = ({ route }) => {
const navigation = useNavigation()
// get the params
const { thing1, thing2 } = route.params
const onPress = () => {
// send to screen 3
navigation.navigate("Screen3", {
thing1,
thing2
})
}
}
EDIT
Oh - i think i realised what you meant by an 'if statement'. You are talking about an effect when you land on the page.
I would say that navigating automatically away from a page is very likely to cause you problems, and may mean you need to combine your screens into one. But to do it you would use an effect:
const Screen1 = () => {
const navigation = useNavigation()
const onPress = () => {
navigation.navigate("Screen2", {
thing1:"my thing",
thing2:"my second thing"
})
}
}
const Screen2 = ({ route }) => {
const navigation = useNavigation()
const isFocused = useIsFocused() // react-navigation
// get the params
const { thing1, thing2 } = route.params
useEffect(() => {
if(thing2 !== thing1 && isFocused){
navigation.navigate("Screen3", {
thing1,
thing2
})
}
}, [thing1, thing2, isFocused]) // <- this means the callback will trigger whenever one changes, including when the component gains focus from react-navigation
}