【发布时间】:2022-11-10 22:00:59
【问题描述】:
我正在使用 redux-persist 开发 React/redux 应用程序,并且我有大量数据要持久化,我希望仅在单击按钮时才能持久化我的 Redux Store,否则我不想存储我的数据。
这是我的商店:
import {
legacy_createStore as createStore,
combineReducers,
applyMiddleware
} from "redux";
import { composeWithDevTools } from "redux-devtools-extension/developmentOnly";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist-indexeddb-storage";
import thunk from "redux-thunk";
import dataReducer from "./reducers/dataReducer";
const reducer = combineReducers({
data: dataReducer
});
const persistConfig = {
key: "STORE",
storage: storage("STORE")
};
const persistedReducer = persistReducer(persistConfig, reducer);
const initialState = {};
const middleware = [thunk];
const composeEnhancers = composeWithDevTools({
features: {
dispatch: true // dispatch custom actions or action creators
}
});
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(
applyMiddleware(...middleware)
// other store enhancers if any
)
);
const persistor = persistStore(store);
export { persistor };
export default store;
通过这样做:redux 从第一次渲染开始持久化数据(这是 indexedDb)
我正在寻找的是仅当我单击按钮并触发persistData 函数时才设置我的indexedDB。
This is my code,所以如果你知道如何实现这一点,请。
【问题讨论】:
-
请问有什么帮助吗?
标签: reactjs redux persist redux-persist