gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:55 24 4
gpt4 key购买 nike

我想将我的一些 reducer 列入黑名单,因为我的状态树越来越大,我收到了这个错误:

Could not write debug session to localStorage: DOMException: Failed to execute 'setItem' on 'Storage': Setting the value of 'redux-persist' exceeded the quota.(…)"

我找到的解决方案是将一些不需要持久化的 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;

但是运行应用程序,我仍然得到黑名单持久化状态,如您在此处所见:

logs of persisted state

我尝试将黑名单 key 更改为:

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

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

最佳答案

我读了这个article在这里,作者提出了一个很好的观点,黑名单不是在你的 redux store 中寻找属性的名称,而是在你的状态中。我有同样的困惑。

在开发工具中仔细检查本地存储中实际保存的内容。确保将正确的名称列入黑名单。

关于javascript - redux-persist 黑名单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46782409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com