0

Here is my code which implements a simple tab navigator. In one of the screens I need to make a request to an API and update the states every time the screen is selected by user via the tab navigator. As shown below the componentDidMount is not executed after first time (Second attempt fails on execution).

export default class UserAccount extends Component {

    constructor(props) {
        super(props)

        this.state = {
            sample:''
        }
    }

    componentDidMount = async () => {

       // Api fetch 
       // ...

       await this.setState({sample: api.response})

    }

    render() {
        return (
            ...
        )
    }
}

And the tab navigator is:

<Tab.Navigator>
    <Tab.Screen options={{
        tabBarIcon: ({ focused }) => (
            <MaterialIcons name="home" color={focused ? activeTintLabelColor : inactiveTintLabelColor} size={23} />
        ),
    }} name="Home" component={Home} />
    <Tab.Screen options={{
        tabBarIcon: ({ focused }) => (
            <FontAwesome5 name="wallet" color={focused ? activeTintLabelColor : inactiveTintLabelColor} size={20} />
        )
    }} name="Wallets" component={UserWallets} />
    
</Tab.Navigator>

What should I do to let the tab navigator re-renders the screen component which causes the execution of componentDidMount?

Regards.

M.reza
  • 3
  • 1
  • 4
  • 1
    This answer will help you. https://stackoverflow.com/a/63610063/3784556 – Sameer Kumar Jain Sep 10 '20 at 09:02
  • 1
    Does this answer your question? [react-navigation: Detect when screen, tabbar is activated / appear / focus / blur](https://stackoverflow.com/questions/50290818/react-navigation-detect-when-screen-tabbar-is-activated-appear-focus-blu) – Sameer Kumar Jain Sep 10 '20 at 09:04
  • 1
    @SaachiTech The first link solved the problem. Thanks. – M.reza Sep 15 '20 at 09:05

0 Answers0