在工作的redux配置上添加redux-persist

时间:2018-10-17 20:30:59

标签: redux react-redux redux-persist

我可以在redux-persist的配置上使用一些帮助。我有一个带有两个中间件的redux存储,配置有效,但是我不能正确添加redux-persist。我设法补水,但动作不再称为减速器,或者我遇到各种错误。我发现redux-persist文档有点差。

这是我当前的配置:

import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import allReducer from './reducers';

export default function configureStore() {
  return createStore(
    allReducer,
    applyMiddleware(thunk, logger)
  );
} 

1 个答案:

答案 0 :(得分:0)

我终于成功了,如果有帮助,这里是要设置的配置:

Store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { persistReducer } from 'redux-persist'
import LocalStorage from 'redux-persist/lib/storage'
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import allReducer from './reducers';

const persistConfig = {
  key: 'xxxx-key-xxxx',
  storage: LocalStorage,
}

const persistedReducer = persistReducer(persistConfig, allReducer)


export default createStore(
  persistedReducer,
  applyMiddleware(thunk, logger)
);

persistStore.js

import { persistStore } from 'redux-persist'
import store from './store';

export default persistStore(store);

App.js

import React, { Component } from 'react';
import { Provider } from 'react-redux'
import { PersistGate } from 'redux-persist/integration/react'
import store from './stores/store';
import persistor from './stores/persistedStore';

import Router from './routes'
import './App.css';

class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <Router/>
        </PersistGate>
      </Provider>
    );
  }
}

export default App;

这里是依赖项和版本的列表

"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-scripts": "2.0.5",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0"

当前命名的导出不起作用,因此我必须为持久性文件创建第二个文件,以便能够为商店和持久性文件使用默认导出。