我想将某些 reducer 列入黑名单,因为我的状态树越来越大,并且我收到此错误:



我发现的解决方案是将一些不需要保留的 reducer 列入黑名单。所以我的App.js中有这个代码

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { persistStore } from 'redux-persist'
import { Initializer } from './components';
import store from './store';

class App extends Component {
  constructor() {
    super()
    this.state = { rehydrated: false }
  }

  componentWillMount(){
    persistStore(store, { blacklist: ['project', 'comment', 'project', 'jobOrder']}, () => {
      this.setState({ rehydrated: true })
    })
  }

render() {
    if(!this.state.rehydrated)
      return <Initializer />;

    return (
      <Provider store={store}>
        <Router>
          <div>
            <Switch>
              ... some Routes
            </Switch>
          </div>
        </Router>
      </Provider>
    );
  }
}

export default App;

我有这个rootReducer:
import { reducer as formReducer } from 'redux-form'

import { combineReducers } from 'redux';
import userAuthReducer from './userAuthReducer';
import jobOrderReducer from './jobOrderReducer';
import clientReducer from './clientReducer';
import userReducer from './userReducer';
import persistReducer from './persistReducer';
import commentReducer from './commentReducer';
import projectReducer from './projectReducer';
import teamReducer from './teamReducer';

export default combineReducers({
    userAuth: userAuthReducer,
    jobOrder: jobOrderReducer,
    job_order: jobOrderReducer,
    client: clientReducer,
    user: userReducer,
    form: formReducer,
    persist: persistReducer,
    comment: commentReducer,
    project: projectReducer,
    team: teamReducer
});

我的persistReducer.js
import { PERSIST } from '../actions/types';

export default (state = [], action) => {
    switch(action.type) {
        case PERSIST:
            return { ...state, ...action.payload }
        default:
            return state;
    }
};

还有我的store.js
import { compose, createStore, applyMiddleware } from 'redux';
import { autoRehydrate } from 'redux-persist';
import thunk from 'redux-thunk';
//import logger from 'redux-logger';

import rootReducer from './reducers';

const store = createStore(
    rootReducer,
    {},
    compose(
        applyMiddleware(thunk, /*logger*/),
        autoRehydrate())
    );

//persistStore(store);
export default store;

但是运行该应用程序后,仍然可以将其列入黑名单的持久状态,如下所示:

javascript - redux-persist黑名单不起作用-LMLPHP

我尝试将黑名单密钥更改为:
persistStore(store, { blacklist: ['reduxPersist:project', 'reduxPersist:comment', 'reduxPersist:project', 'reduxPersist:jobOrder']}, () => {
      this.setState({ rehydrated: true })
    })

但是关键仍然存在...如何正确执行此操作?

最佳答案

我在这里阅读了此article,作者提出了一个很好的观点,黑名单不是在redux存储中查找属性名称,而是在您的状态中查找。我也有同样的困惑。

仔细检查开发工具,实际上什么是保存在本地存储中。确保您将正确的名称列入黑名单。

07-24 09:46
查看更多