rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.94k stars 866 forks source link

PersistGate onBeforeLift does not gate #921

Closed mzafer closed 5 years ago

mzafer commented 5 years ago

I have the below code in my component and I want to prevent App from being mounted and rendered before the settings are loaded. My understanding is the onBeforeLift will gate the App from being loaded till the loadSettings method is completed but the App is rendered before that.

return (

this.loadSettings()} persistor={persistor}>
      </View>
    );

Below is my loadSettings function, The SettingsRepo.getSettings() returns a promise.

async function loadSettings() { await SettingsRepo.getSettings(); return; }

Am I doing anything wrong ? How do I prevent App from being mounted till the Settings are loaded ?

aguynamedben commented 5 years ago

Can you provide a test repo that demonstrates the issue and I'll help look into it?

I think it might be because your OnBeforeLift argument is a function that is simply firing, then PersistGate moves on, without waiting for the Promise to resolve. Can you try...

return (
  <PersistGate
    onBeforeLift={ async () => { await this.loadSettings() } }
    persistor={persistor}
  />
);

or maybe just

return (
  <PersistGate
    onBeforeLift={this.loadSettings()}
    persistor={persistor}
  />
);

I believe onBeforeLift can take a function or a Promise to resolve.

aguynamedben commented 5 years ago

@mzafer Let me know if you still are having a hard time with this and I'll re-open.