zustand-persist icon indicating copy to clipboard operation
zustand-persist copied to clipboard

screen is blank after using PersistGate in react native

Open krunaldodiya opened this issue 3 years ago • 8 comments

import React, {memo} from 'react';
import {PersistGate} from 'zustand-persist';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';

export default App() {
  const RootStack = createStackNavigator();

  const {initialScreen} = authStore(state => state);

  return  (
     <PersistGate>
          <NavigationContainer>
                <RootStack.Navigator initialRouteName={initialScreen}>
                  <RootStack.Screen
                    name="Auth"
                    component={Auth}
                  />
                </RootStack.Navigator>
              </NavigationContainer>
        </PersistGate>
    )
}

krunaldodiya avatar Jul 24 '21 16:07 krunaldodiya

Same here. However, I only have a blank screen when I run my app on IOS. On Android, it works 100%.

mbuxmann avatar Sep 02 '21 12:09 mbuxmann

Did you try loading prop? in <PersistGate>. It will show the component while getting saved data from storage

roadmanfong avatar Nov 03 '21 12:11 roadmanfong

Hey @krunaldodiya and @mbuxmann, I'm going to have a go at fixing this. Did either of you have a loading component configured? If you did, it would help if you can confirm whether you were stuck on your loading component, or whether your loading component never showed up at all.

smallsaucepan avatar Nov 04 '21 01:11 smallsaucepan

Hi @smallsaucepan, unfortunately, I didn't have a loading component configured.

mbuxmann avatar Nov 04 '21 18:11 mbuxmann

Simulator Screen Shot - iPhone 13 - 2021-11-24 at 20 12 54

<PersistGate onBeforeLift={() => { console.log('ON BEFORE EVENT'); }} loading={ <View style={{ flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }}> <Text>LOADING!</Text> </View> }> <AppNavigation /> </PersistGate>

Page is not navigating to inner screens. How should that be configured

pku-arun avatar Nov 24 '21 14:11 pku-arun

Any update on this?

souzaramon avatar Feb 08 '22 04:02 souzaramon

Hi @pku-arun, can you please post your entire code? Are you calling useStore() somewhere prior to rendering the PersistGate? Thanks

smallsaucepan avatar Feb 15 '22 10:02 smallsaucepan

Think I've figured this out. @roadmanfong would you mind reading along to see if the diagnosis and planned fix make sense?

The PersistGate works by setting a custom onAllLoaded callback which sets isReady state to true, lifting the gate. However, if the LoadManager finishes loading the data before the PersistGate renders the default onAllLoaded callback is executed - a noop. This means PersistGate's callback never gets called, and isReady never gets set to true.

Proposed solution is to change LoadManager so that if onAllLoaded is called after all state is loaded, it executes the callback immediately. PersistGate calls onAllLoaded from inside useEffect to avoid calling it multiple times and re-rendering.

If that makes sense I'll work on a PR.

smallsaucepan avatar Mar 11 '22 03:03 smallsaucepan

Resolved by PR #19

smallsaucepan avatar Jul 23 '23 12:07 smallsaucepan