Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for async before register component in react-native

Tags:

react-native

I need to wait for async storage and then init app, because I store auth token here and want to show correct scene for user if he was authorised:

(async () => {
  const viewer = JSON.parse(await AsyncStorage.getItem('viewer'));

  // ... 

  const RootContainer = () => (
    // ...
  );

  AppRegistry.registerComponent('yawaloo', () => RootContainer);
})();

I have moved to react-native 0.40.0 from 0.34.1 and now have an error "Module AppRegistry is not a registered callable".

In previous version everything were ok. How can I wait for some actions and then start render RootContainer?

like image 866
Tony Avatar asked Oct 30 '22 13:10

Tony


1 Answers

One idea is to use splash screen. More specifically use a state in your RootContainer to determine whether to show a splash screen or your main UI. Set the state to false (show splash) initially then after you read the token from async storage, then set the state to true.

Part of why apps have splash screens is to deal with situation like this. HTH

like image 142
idealllee Avatar answered Nov 03 '22 00:11

idealllee