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

PersistGate causing slowness

Open adhenrique opened this issue 6 years ago • 21 comments

In development environment, I noticed that with each reload that the app was slow. So I did some testing, and by removing PersistGate, the app loads easily, and without any slowness.

cfgStore:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

import reducers from './combineStore';

const persistConfig = {
    key: 'root',
    storage,
};

const cfgStore = () => {
    const middlewares = [thunk];
    const enhancer = applyMiddleware(...middlewares);
    const persistedReducer = persistReducer(persistConfig, reducers);

    // create store
    return createStore(persistedReducer, enhancer);
};

export const persistor = persistStore(cfgStore());

export default cfgStore;

index.js

// ...

export default class App extends Component {
    componentDidMount(){
        SplashScreen.hide()
    }

    render() {
	// ...
        return (
            <Provider store={store}>
                <PersistGate loading={null} persistor={persistor}>
                    <StatusBar
                        backgroundColor={mainStyle.principalBackColor}
                        barStyle="dark-content"
                    />
                    <Loader loading={loader}/>
                    <RouterWithRedux>
                        <Scene key='root'>
                            <Scene
                                component={List}
                                // initial={isLoggedIn && token}
                                initial={true}
                                hideNavBar={true}
                                key='List'
                                title='List'
                            />
                        </Scene>
                    </RouterWithRedux>
                </PersistGate>
            </Provider>
        )
    }
}

My stack: react-native 0.54.4 react-native-router-flux ^4.0.0-beta.28 react-redux ^5.0.7 redux-persist ^5.9.1 redux-thunk ^2.2.0

adhenrique avatar May 02 '18 01:05 adhenrique

I faced the same issue

workostap avatar May 10 '18 13:05 workostap

@adhenrique how did you resolved this issue?

waliurjs avatar Jun 03 '18 11:06 waliurjs

Having the same problem. Could anyone pinpoint to a solution?

waclock avatar Feb 04 '19 16:02 waclock

Any hint here ?

el-lsan avatar Apr 11 '19 15:04 el-lsan

why this one is closed? facing same issue

chaiyilin avatar May 12 '19 23:05 chaiyilin

Having the same problem. Any solutions?

sreehari95 avatar Jul 03 '19 04:07 sreehari95

It's been 1 year since I posted this problem here. I will try to run it again, and I will comment again here.

if I'm not mistaken, it was something related to configuration ...

adhenrique avatar Jul 03 '19 04:07 adhenrique

@adhenrique did you find something? I created this feature request, I don't know if it's possible, but I'll surely look it this weekend. https://github.com/rt2zz/redux-persist/issues/1094

nishanBende avatar Sep 24 '19 12:09 nishanBende

version 6 has timeout option in config and you can reduce 5 seconds to whatever you want (risking you state not being properly hydrated I guess)

kompot avatar Nov 11 '19 12:11 kompot

version 6 has timeout option in config and you can reduce 5 seconds to whatever you want (risking you state not being properly hydrated I guess)

what's the meaning of timeout?

anc95 avatar Jun 04 '20 04:06 anc95

Try this: const persistConfig = { timeout: 2000, //Set the timeout function to 2 seconds key: 'root', storage, };

GitPhillip avatar Jan 09 '21 20:01 GitPhillip

Try this: const persistConfig = { timeout: 2000, //Set the timeout function to 2 seconds key: 'root', storage, };

This works for me! thanks @GitPhillip

leonarpv avatar Mar 03 '21 19:03 leonarpv

Is the timeout option documented anywhere? I can't find it mentioned anywhere in any of the official docs. What does it actually do?

liquidvisual avatar Apr 15 '22 09:04 liquidvisual

Is the timeout option documented anywhere? I can't find it mentioned anywhere in any of the official docs. What does it actually do?

Remember PersistGate delays the rendering of your app's UI until your persisted state has been retrieved and saved to redux.

It can happen that your UI loads before your state is ready and that causes problems. The timeout option allows you to set the delay time that is otherwise 5 seconds at default (if I'm not mistaken)

GitPhillip avatar Apr 22 '22 15:04 GitPhillip

Is there any solution other than guessing a number that could work? Something like "as soon as state is ready, render UI" instead of waiting for a timeout?

baltagih2 avatar May 06 '22 18:05 baltagih2

Is there any solution other than guessing a number that could work? Something like "as soon as state is ready, render UI" instead of waiting for a timeout?

persistStore(store, [config, callback])

arguments -store (redux store): The store to be persisted. -config object (typically null)

If you want to avoid that the persistence starts immediately after calling persistStore, set the option manualPersist. Example: { manualPersist: true } Persistence can then be started at any point with persistor.persist(). You usually want to do this if your storage is not ready when the persistStore call is made. callback function will be called after rehydration is finished. returns persistor object

GitPhillip avatar May 10 '22 03:05 GitPhillip

Youp the timeout fixed it

WinterDevelopers avatar May 22 '23 16:05 WinterDevelopers

Try this: const persistConfig = { timeout: 2000, //Set the timeout function to 2 seconds key: 'root', storage, };

thanks it worked for me and i kept it to 100ms

bedukumar avatar Jul 30 '23 13:07 bedukumar

Try this: const persistConfig = { timeout: 2000, //Set the timeout function to 2 seconds key: 'root', storage, };

Yeah It solved. But not for the routes like '/resetPassword?code=...' I am Using Nextjs with Strapi server so I need to navigate to /resetPassword route from my email which also contains code in route query. Is there anay hack to do so?

MehdiRazaNaqvi avatar Aug 15 '23 22:08 MehdiRazaNaqvi

Is there any solution other than guessing a number that could work? Something like "as soon as state is ready, render UI" instead of waiting for a timeout?

persistStore(store, [config, callback])

arguments -store (redux store): The store to be persisted. -config object (typically null)

If you want to avoid that the persistence starts immediately after calling persistStore, set the option manualPersist. Example: { manualPersist: true } Persistence can then be started at any point with persistor.persist(). You usually want to do this if your storage is not ready when the persistStore call is made. callback function will be called after rehydration is finished. returns persistor object

Man this solved my problem in a sensable way. without changing timeout which makes no sense i used manualPersist option and called persistor.persist() inside a useEffect callback which i presume runs after UI has loaded and in this way the delay has totally gone.

BaalGroup avatar Dec 04 '23 09:12 BaalGroup

Is there any solution other than guessing a number that could work? Something like "as soon as state is ready, render UI" instead of waiting for a timeout?

persistStore(store, [config, callback])

arguments -store (redux store): The store to be persisted. -config object (typically null)

If you want to avoid that the persistence starts immediately after calling persistStore, set the option manualPersist. Example: { manualPersist: true } Persistence can then be started at any point with persistor.persist(). You usually want to do this if your storage is not ready when the persistStore call is made. callback function will be called after rehydration is finished. returns persistor object

can you point to any documentation on this.? I see nothing online about manually persisting the store.

sheldonNico10 avatar Apr 15 '24 00:04 sheldonNico10