我正在使用 Redux Persist 来保存应用程序的状态,以便在它关闭并再次打开时它是相同的。初始状态已成功保存,但我似乎无法通过操作更新持久状态。我的代码如下:
应用程序.js
import React from "react";
import { createStore } from "redux";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import reducers from "./src/reducers";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import Router from "./src/Router";
const persistConfig = {
key: "root",
storage,
debug: true
};
const persistedReducer = persistReducer(persistConfig, reducers);
const store = createStore(persistedReducer);
const persistor = persistStore(store);
const App = () => (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Router />
</PersistGate>
</Provider>
);
export default App;
调度减速器
import { strings } from "../../locales/i18n";
import * as types from "../actions/types";
const initialState = strings("schedule.list").map((item, index) => {
return {
key: index.toString(),
title: item.title,
time: item.time,
location: item.location,
description: item.description,
isFavorite: false
};
});
const scheduleReducer = (state = initialState, action) => {
switch (action.type) {
case types.TOGGLE_FAVORITE:
state.map(schedule => {
if (schedule.key === action.id) {
return (schedule.isFavorite = !schedule.isFavorite);
}
});
return state;
default:
return state;
}
};
export default scheduleReducer;
当我调用操作时,我可以看到isFavorite
更改的状态,但是当我重新加载应用程序时,它并没有持久化。这里可能是什么问题?