【问题标题】:React Redux Persist : How to control when persist data in indexeddbReact Redux Persist:如何控制何时在 indexeddb 中持久化数据
【发布时间】: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;

我的 Index.js 看起来像这样:

我创建了一个组件来显示我的数据:

通过这样做:redux 从第一次渲染开始持久化数据(这是 indexedDb)

我正在寻找的是仅当我单击按钮并触发persistData 函数时才设置我的indexedDB。

This is my code,所以如果你知道如何实现这一点,请。

【问题讨论】:

  • 请问有什么帮助吗?

标签: reactjs redux persist redux-persist


【解决方案1】:

我来这里是为了寻找这个完全相同的问题的答案。我正在使用 Redux-persist,但保存在 localStorage 中的数据是初始状态中的数据。我通常会在会话期间进行一些更改,并且我希望能够有一种方法将其手动保存到 Redux-persist 信息中。我知道使用 Hydrate 方法必须是可能的,但我只是一个初学者,我不知道如何进行。如果有人可以提供帮助,那就太好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多