S. N
S. N

Reputation: 3949

react native splash page warning

I have made an splash page looks like this:

export default class Splash extends Component {
  performTimeConsumingTask = async () => {
    return new Promise((resolve) =>
      setTimeout(() => {
        resolve('result');
      }),
    );
  };

  async componentDidMount() {
    const data = await this.performTimeConsumingTask();
    // const navigation = useNavigation();
    if (data !== null) {
      this.props.navigation.navigate('BottomMainNavgigation');
    }
  }

  render() {
    return (
      <View style={styles.viewStyles}>
        {/* <Text style={styles.textStyles}>Welcome</Text> */}
        <Image
          style={styles.tinyLogo}
          source={{
            uri: URL.logov2,
          }}
        />
      </View>
    );
  }
}

THen I use this like this, in my navigation:

const RootStackScreen = (props) => {
  const [t] = useTranslation();
  return (
    <SplashRootStack.Navigator>
      <SplashRootStack.Screen
        name="Main"
        component={Splash}
        options={{headerShown: false, headerBackTitle: t('back')}}
      />
      <SplashRootStack.Screen
        name="BottomMainNavgigation"
        component={BottomMainNavgigation}
        options={{headerShown: false, headerBackTitle: t('back')}}
      />
    </SplashRootStack.Navigator>
  );
};

and also:

<PaperProvider theme={MyTheme}>
  <NavigationContainer linking={linking} fallback={<Splash />}>
    <RootStackScreen />
  </NavigationContainer>
</PaperProvider>

and like this in my app.js:

const App = () => {
  return (
    <Provider store={store}>
      <PersistGate loading={<Splash />} persistor={persistor}>
        <Suspense fallback={<Splash />}>
          <Navigation />
        </Suspense>
      </PersistGate>
    </Provider>
  );
};

export default App;

when I run the application it looks like this:

enter image description here

there is a warning :

enter image description here

I get this warning with id= 0, 1 and 2 and I get this warning also:

enter image description here

what have I donr incorrectly ad how can I remove these warnings, also when I load the app in the emulator, I get a white screen for few seconds before I get my own splash page and then to my app.

how can I do this better?

Upvotes: 1

Views: 182

Answers (2)

Waheed Akhtar
Waheed Akhtar

Reputation: 3187

You are using Splash component in redux persist as a loader and in your Splash component there isn't any navigation prop available because it's a parent component and not the part of navigation tree you need to use switch navigator for the same purpose but with the current structure navigation will not work unless you move navigation part inside the navigator tree. Now the solution is,

  • Use only splash as a static UI component.
  • Move you navigation or componentDidMount logic inside the stack navigator.

Add simple Activity indicator as fallback.

<PersistGate
    loading={<ActivityIndicator style={{top: '45%'}}
      animating color={theme.appColor} size='large' />}
    persistor={ReduxStore.persistor}>
    <Navigator />
</PersistGate>

Upvotes: 1

Guruparan Giritharan
Guruparan Giritharan

Reputation: 16334

Your warnings

  1. Undefined is not an object : The problem is that you are using the Splash as the fallback component so until your deeplink is resolved Splash would be displayed and the Splash here is not part of navigation which will not get the 'navigation' prop so you are getting the warning. Same for the other higher order components like PersistGate and suspense you can given the splash for everything and all this is outsider navigation. resolution : Use the activity indicator instead of splash for the fallback

  2. This is due to one of your middleware in redux taking longer, better check your redux middleware.

  3. White screen, this is whats causing the white screen maybe caused by the same reason as your middleware warning or the component did mount of the splash screen. And you have several providers so better remove one or two and check whats causing that.

You can check this sample to get an idea on using splash screens and authentication. https://snack.expo.io/@guruparan/rnn-v5

Upvotes: 1

Related Questions