Мое состояние не сохраняется, функция сброса возвращает массив нулей вместо разрешенных обещаний.
Это пакеты, которые я использую:
- redux: v4.0.4
- response-redux: v7.1.1
- redux-persist: v6.0.0
- электронный магазин: v5.0.0
Это моя конфигурация редукторов:
const appReducer = (history) => combineReducers({
user: persistReducer(persistedConfsByKeys.user, userReducer),
envs: persistReducer(persistedConfsByKeys.envs, envs),
router: connectRouter(history),
});
const persistedConfsByKeys = Ru.map(persistConfig, persistedReducerByKeys);
const persistedReducerByKeys = {
user: {
key: 'user'
},
envs: {
key: 'envs',
throttle: 3
},
router: {
key: 'router'
}
};
const persistConfig = (spec) => {
const { key } = spec;
const runMigration = getMigrationRunnerFn(key);
const electronStore = new ElectronStore();
return {
key: 'persist:'+key,
version: 2,
storage: createElectronStorage({ electronStore }),
serialize: false,
debug: true,
migrate: (undefinedState, version) => {
const state = storeService.store.getState();
const reducerState = state[key];
if (Ru.isNil(state)) {
return B.resolve(state)
}
const stateVersion = Ru.path(['_persist', 'version'], state);
if (Ru.isNotNil(stateVersion) && stateVersion >= version) {
return B.resolve(state);
}
runMigration(reducerState))
return (
runMigration(reducerState)
.tap(newState => {
console.log('key', key)
console.log('newstate', newState)
newState = Ru.assocPath(['_persist', 'version'], version, newState);
electronStore.set('persist:'+key, newState);
})
)
}
};
};
Этомой магазин:
const history = createHashHistory({
hashType: 'slash',
getUserConfirmation: (message, callback) => callback(window.confirm(message))
});
const initPersistentStore = (initialState, enhancer) => {
const store = createStore(rootReducer(history), initialState, enhancer);
return { store, persistedRootReducer: rootReducer };
};
// create store
const { store, persistedRootReducer } = initPersistentStore(initialState, enhancer);
import ElectronStore from 'electron-store';
import rootSelectors from 'reducers/selectors';
import persistor from '../../../../app';
const setUserDataOnReduxStorage = (userId, data) => {
const electronStore2save = new ElectronStore({
name: `users/${userId}/config`
});
const userData2save = rootSelectors.userData(data);
electronStore2save.set(userData2save);
};
const getUserDataFromReduxStorage = userId => {
const electronStore = new ElectronStore({ name: `users/${userId}/config` });
const result = electronStore.get();
return result;
};
const forceUpdateReduxPersistance = () => {
console.log('---------- persistor ----------', persistor)
persistor.flush()
.then(response => console.log('*********** response ************', response))
};
export {
setUserDataOnReduxStorage,
getUserDataFromReduxStorage,
forceUpdateReduxPersistance
};
export default {
setUserDataOnReduxStorage,
getUserDataFromReduxStorage,
forceUpdateReduxPersistance
};
Это приложение index.js, где я использую persistGate
class Root extends Component {
render() {
return (
<Provider store={this.props.store} context={ReactReduxContext}>
<PersistGate loading={null} persistor={persistor}>
<ConnectedRouter history={this.props.history} context={ReactReduxContext}>
<AddapsBase {...this.props} />
</ConnectedRouter>
</PersistGate>
</Provider>
);
}
}