I am using react-native with react-native-navigation. I would like to reload data when a component is shown. The component is shown when a user clicks on a tab navigation button.
Should I use react life cycle events or is there something in react-native-navigation that can trigger a function when a user navigates back to a component?
I am using redux, I am not sure if that could be used to help?
This issue refers to onDisplay
which seems like what I am looking for. However I can't find any official documentation about it - https://github.com/wix/react-native-navigation/issues/130
There are two approaches to calling an action on screen focusing: Using the withNavigationFocus higher order component provided by react-navigation. Listening to the 'didFocus' event with an event listener.
use componentWillMount() method. This will execute automatically before render() method gets triggered. @Jaydip componentWillMount() will be called before the render method is invoked.
Forcing an update on a React class component In any user or system event, you can call the method this. forceUpdate() , which will cause render() to be called on the component, skipping shouldComponentUpdate() , and thus, forcing React to re-evaluate the Virtual DOM and DOM state.
I like the solution proposed by Bruno Reis. I tweaked mine to make it a bit simpler.
class Whatever extends Component {
componentDidMount(){
this.load()
this.props.navigation.addListener('willFocus', this.load)
}
load = () => {
...
}
}
In the documentation, it shows that you can add a focus event like this:
import React, { useEffect } from 'react'
const MyComponent = ({ navigation }) => {
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
// do something
console.log('Hello World!')
});
return unsubscribe;
}, [navigation]);
return (
<View>
<Text>MyComponent</Text>
</View>
)
}
export default MyComponent
include this as 'callIfBackToThisRoute'...
export default ( props, call ) => {
if( !props.navigation ) throw 'I need props.navigation'
const thisRoute = props.navigation.state.routeName;
props.navigation.addListener(
'willFocus',
payload => {
if( payload.state.routeName == thisRoute) call(props)
}
);
}
and use it inside your component...
componentDidMount() {
const { doIt } = this.props;
doIt()
callIfBackToThisRoute(
this.props,
(props) => doIt()
)
}
For RNN v3, after trying out for quite a couple times, I finally figured out the correct way:
componentDidMount() {
this.navigationEventListener = Navigation.events().bindComponent(this);
}
componentWillUnmount() {
if (this.navigationEventListener) {
this.navigationEventListener.remove();
}
}
componentDidAppear() { // Lazy loading data for RNN
if (this.state.routes.length === 0) {
this.getData();
}
}
The key is that, the binding of event listener is mandatory, otherwise the componentDidAppear() and componentDidDisappear() won't be triggered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With