react-redux 持久数据存储

1、

npm i redux-persist --save

 

2、store/index

import { createStore, combineReducers } from ‘redux‘;
import reducers from ‘./reducers/index‘;
import {persistStore, persistReducer} from ‘redux-persist‘;
import storage from ‘redux-persist/lib/storage‘;

const persistConfig = {
  key: ‘healthRoot‘,
  storage: storage
};

const _reducers = persistReducer(persistConfig, combineReducers(reducers))
const store = createStore(_reducers)

export const persistor = persistStore(store)
export default store

 

3、index 根目录

import React from ‘react‘;
import ReactDOM from ‘react-dom‘;
import { Provider } from ‘react-redux‘;
import store from ‘./store/index‘;
import {persistor} from ‘./store/index‘
import {PersistGate} from ‘redux-persist/lib/integration/react‘;
import ‘assets/css/index.less‘;
import "lib-flexible";
import ‘./libs/TkpAxios‘;
import ‘./mock‘;
import App from ‘./App‘;
import { BrowserRouter } from ‘react-router-dom‘;
import ‘src/base/index‘;

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <BrowserRouter>
          <App />
        </BrowserRouter>
      </PersistGate>
    </Provider>
  </React.StrictMode>,
  document.getElementById(‘root‘)
);

 

 

参考

https://www.npmjs.com/package/redux-persist

https://segmentfault.com/a/1190000018150177

react-redux 持久数据存储

上一篇:Dos命令


下一篇:2.环境搭建