Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to initialize data loading with react-navigation

I'm using react-navigation and here is my structure :

The root stack navigator :

export const Root = StackNavigator({
Index: {
    screen: Index,
    navigationOptions: ({ navigation }) => ({

    }),
},
Cart: {
    screen: Cart,
    navigationOptions: ({ navigation }) => ({
        title: 'Votre panier',
        drawerLabel: 'Cart',
        drawerIcon: ({ tintColor }) => <Icon theme={{ iconFamily: 'FontAwesome' }} size={26} name="shopping-basket" color={tintColor} />
    }),
},
...

My structure looks like this :

  • StackNavigator (Root)
    • DrawerNavigator (Index)
      • TabNavigator
        • MyPage
        • MyPage (same page formatted with different datas)
        • ...

So my question is, where do I load my data, initialize my application ? I need somewhere called once, called before the others pages.

The first page displayed in my application is the MyPage page. But as you can see, because of the TabNavigator, if I put my functions inside, it will be called many times.

Some will says in the splashscreen, but I'm using the main splashscreen component and I don't have many controls over it.

I thought about my App.js where we create the provider, but I don't think this is a good idea ?

const MyApp = () => {

    //TODO We're loading the data here, I don't know if it's the good decision
    ApplicationManager.loadData(store);
    SplashScreen.hide();

    return (
        <Provider store={store}>
            <Root/>
        </Provider>
    ); 
};

What is the good way to do it ?

like image 789
FR073N Avatar asked Nov 02 '17 08:11

FR073N


People also ask

How do I set initial screen in react navigation?

Use initialRouteName props (The name of the route to render on first load of the navigator) if you want a specific screen, otherwise the first stack screen will be rendered by default.

How do you pass data while navigation is in react?

Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function: navigation.navigate('RouteName', { /* params go here */ })

What is the use of NavigationContainer?

The NavigationContainer is responsible for managing your app state and linking your top-level navigator to the app environment. The container takes care of platform specific integration and provides various useful functionality: Deep link integration with the linking prop.

Why my react navigation is not working?

A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native. ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication).


1 Answers

class MyApp extends Component {

  state = {
    initialized: false
  }

  componentWillMount() {
    // if this is a promise, otherwise pass a callback to call when it's done
    ApplicationManager.loadData(store).then(() => {
      this.setState({ initialized: true })
    })
  }

  render() {
    const { initialized } = this.state
    if (!initialized) {
      return <SplashScreen />
    }
    return (
      <Provider store={store} >
        <Root />
      </Provider>
    );
  }
}
like image 143
Alexander Vitanov Avatar answered Sep 20 '22 09:09

Alexander Vitanov