Is it possible to update a tab with React navigation whenever I access it?

Asked

Viewed 355 times

-1

It is possible to update a tab with React navigation whenever I access it?

for example I have 2 guides "Home" and "Settings"...

If in the home tab I use navigation.Navigator('Settings'), the guide goes to Settings, but if Settings has been rendered before, it will not render again.

have to leave in automatic whenever I access Settings, force the tab to render again? ( updating states, etc...)

1 answer

1

When you open the screen for the first time, it runs the "IdidMount" to mount the screen, if you continue browsing, it will no longer run.

You can change this behavior by checking if the screen has received new props, or by adding a "Systener" when assembling the screen for the first time, so that whenever it receives the focus some programming is executed. Here is an example:

import React, { Component } from 'react';
import { View } from 'react-native';
import { withNavigation } from 'react-navigation';

class TabScreen extends Component {
  componentDidMount() {
    const { navigation } = this.props;
    this.focusListener = navigation.addListener('didFocus', () => {
      // Executar alguma ação
    });
  }

  componentWillUnmount() {
    // Remove o listener ao desmontar
    this.focusListener.remove();
  }

  render() {
    return <View />;
  }
}

export default withNavigation(TabScreen);

Here is more information:

https://reactnavigation.org/docs/en/function-after-focusing-screen.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.