is there a way to pass param from navigate.goback() and parent can listen to the params and update its state?
You can pass a callback function as parameter (as mentioned in other answers).
Here is a more clear example, when you navigate from A to B and you want B to communicate information back to A you can pass a callback (here onSelect):
The best solution is using NavigationEvents. You don't need to create listeners manually.
Calling a callback function is not highly recommended. Check this example using a listener (Remember to remove all listeners from componentWillUnMount with this option).
Component A:
navigateToComponentB() {
const { navigation } = this.props
this.navigationListener = navigation.addListener('willFocus', payload => {
this.removeNavigationListener()
const { state } = payload
const { params } = state
//update state with the new params
const { otherParam } = params
this.setState({ otherParam })
})
navigation.push('ComponentB', {
returnToRoute: navigation.state,
otherParam: this.state.otherParam
})
}
removeNavigationListener() {
if (this.navigationListener) {
this.navigationListener.remove()
this.navigationListener = null
}
}
componentWillUnmount() {
this.removeNavigationListener()
}
Each param, route, and navigation state must be fully
JSON-serializable for this feature to work. This means that your
routes and params must contain no functions, class instances, or
recursive data structures.
I like this feature in Development Mode and when I pass params as function I simply can't use it
// use this method to call FirstScreen method
execBack(param) {
this.props.navigation.state.params.updateData(param);
this.props.navigation.goBack();
}
With React Navigation v5, just use the navigate method. From the docs:
To achieve this, you can use the navigate method, which acts like
goBack if the screen already exists. You can pass the params with
navigate to pass the data back
import { useFocusEffect } from '@react-navigation/native';
useFocusEffect(
React.useCallback(() => {
// YOUR CODE WHEN IT IS FOCUSED
return // YOUR CODE WHEN IT'S UNFOCUSED
}, [userId])
);
Easiest way to render the required components is by using useIsFocused hook.
React Navigation provides a hook that returns a boolean indicating whether the screen is focused or not. The hook will return true when the screen is focused and false when our component is no longer focused.
First import this in the required page where you want to navigate back.
import { useIsFocused } from '@react-navigation/native';
Then, store this in any variable, and render components changes using React useEffect hook.
I could not get any answer to work for my specific use case. I have a list being fetched from a database and a screen to add another list item. I wanted that once a user creates the item on the second screen, the app should navigate back to the first screen and show the newly added item in the list. Although the item was being added in the database, the list was not updating to reflect the change. The solution that worked for me: https://github.com/react-navigation/react-navigation.github.io/issues/191#issuecomment-641018588
So all I did was put this on the first screen and now the useEffect is triggered every time the screen is in focus or loses focus.
import { useIsFocused } from "@react-navigation/native";
const isFocused = useIsFocused();
useEffect(() => {
// Code to run everytime the screen is in focus
}, [isFocused]);
This gets the job done. However, this method will be executed every time the screen is rendered.
In order to only render it once when navigating back you can do the following:
With react-navigation version 6 I implemented it by passing a parameter while navigating to another page then calling that parameter which is function beside goBack() function like this