React-navigation call function on screen change

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


React-navigation call function on screen change



I need to acquire data from the server to load user set settings when the user changes screen. It is done by a function in the class. I have tried:


componentWillMount(){
console.log("Sent to function");
this.onSaveCats('initialize');
}



What does not post anything to console log. I have not found a good solution for triggering a function when a new screen is changed. Is there something I am missing?


"dependencies": {
"expo": "^20.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.47.0",
"react-native-radio-buttons": "^1.0.0",
"react-navigation": "^1.0.0-beta.11"
}





tabBarOnPress() from the TabNavigator component can be used reactnavigation.org/docs/navigators/tab.
– Stophface
Sep 12 '17 at 15:28


TabNavigator





I think you're on right way, but what about the screens? Is it seperate classes? You need to use that approach in every of this class
– user14511
Sep 12 '17 at 15:33




1 Answer
1



In react navigation, we can subscribe to listeners and add corresponding callbacks.
The four events that we can subscribe to listeners on are:
willFocus, willBlur, didFocus and didBlur.



Usage in this case:


export default class AnyScreen extends Component{
constructor(props){
super(props);
const didBlurSubscription = this.props.navigation.addListener(
'didBlur',
payload => {
console.debug('didBlur', payload);
});
}
}



You can add similar subscription for didFocus as well by substituting 'didBlur' to any of the other options.



Once you're done listening, you can even remove the listener subscription:


didBlurSubscription.remove();



Hope this helps!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP