redux-offline app flickers the InitialState values on first load

419 Views Asked by At

I'm using redux-offline in my React app and things look good except that the values "flicker" when the page first loads. For less than a second, I can see the initial value of a store variable before it's replaced with the persisted value.

Is there a fix for this?

Here is my store code:

import { reducer as reduxFormReducer, FormStateMap } from 'redux-form';
import { connectRouter, routerMiddleware, RouterState } from 'connected-react-router';
import { combineReducers, createStore, applyMiddleware, Store, compose } from 'redux';
import { History, createBrowserHistory } from 'history';
import { offline, createOffline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
import thunk from 'redux-thunk';

// state
export interface IAppState {
    readonly form: FormStateMap;
    readonly router: RouterState;
    ...
}

// tslint:disable-next-line:no-empty
export const neverReached = (never: never) => {};

export const history = createBrowserHistory();

const rootReducer = ((history: History) => combineReducers<IAppState>({
    form: reduxFormReducer,
    router: connectRouter(history),
    ...
}))(history);

const { middleware, enhanceReducer, enhanceStore } = createOffline(offlineConfig);

export function configureStore(): Store<IAppState> {
    // This line is suspect, not sure if this is the middleware required
    const store = createStore(
        enhanceReducer(rootReducer), 
        undefined,
        compose(
            applyMiddleware(middleware, routerMiddleware(history), thunk),
            enhanceStore));

    return store;
}
1

There are 1 best solutions below

0
azundo On BEST ANSWER

You can keep track of when state has been rehydrated by updating redux state based on the redux-persist REHYDRATE action. redux-offline re-exports the action type constant as PERSIST_REHYDRATE. A simple reducer like the one below should suffice.

import {PERSIST_REHYDRATE} from '@redux-offline/redux-offline/lib/constants';

function rehydratedReducer = (state = {rehydrated: false}, action) {
  if (action.type === PERSIST_REHYDRATE) {
    return {rehydrated: true};
  }
  return state;
}