import { createStore } from 'redux';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import reducer from './reducer';
let persistedReducer = persistReducer({ key: 'auto', storage }, reducer);
let store = createStore(persistedReducer);
export default store;
persistReducer(config, reducer)
arguments
config object
required config: key, storage
notable other config: whitelist, blacklist, version, stateReconciler, debug
reducer function
any reducer will work, typically this would be the top level reducer returned by combineReducers
returns an enhanced reducer
import React from "react";
import { HashRouter as Router, Route } from "react-router-dom";
import { PersistGate } from 'redux-persist/integration/react';
import { persistStore } from 'redux-persist';
import store from '../redux/store';
import { Provider } from 'react-redux';
import Index from '../redux/index';
import Login from '../redux/login';
let persistor = persistStore(store);
export default (
<PersistGate loading={null} persistor={persistor}>
<Router>
<Provider store={store}>
<div>
<Route exact path='/' component={Index}></Route>
<Route exact path='/login' component={Login}></Route>
</div>
</Provider>
</Router>
</PersistGate>
);
persistStore(store, [config, callback])
arguments
store redux store The store to be persisted.
config object (typically null)
callback function will be called after rehydration is finished.
returns persistor object
persistor object
the persistor object is returned by persistStore with the following methods:
.purge()
purges state from disk and returns a promise
.flush()
immediately writes all pending state to disk and returns a promise
.pause()
pauses persistence
.persist()
resumes persistence
评论 (0)