简体   繁体   English

我如何在 react.js 中坚持 redux

[英]How do i do redux persist in react.js

im working on a project where i want to persist redux state for my cart but seems to be missing some thing.我正在做一个项目,我想为我的购物车坚持 redux state 但似乎缺少一些东西。 this is my store:这是我的商店:

import { createStore } from "redux";
import counterReducer from "../store/reducers/reducers";
import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage"; // defaults to localStorage for web

// const store = createStore(counterReducer);

// export default store;

const persistConfig = {
  key: "root",
  storage,
};

const persistedReducer = persistReducer(persistConfig, counterReducer);

export default () => {
  let store = createStore(persistedReducer);
  let persistor = persistStore(store);
  return { store, persistor };
};

and this is my index.js:这是我的 index.js:

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import "bootstrap/dist/css/bootstrap.min.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { Provider } from "react-redux";
import store from "./store/";
import persistor from "./store/";
import { PersistGate } from "redux-persist/integration/react";

ReactDOM.render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <App />
    </PersistGate>
  </Provider>,
  document.getElementById("root")
);

serviceWorker.unregister();

this is the error im getting when i try to run my app:这是我尝试运行我的应用程序时遇到的错误:

TypeError: store.getState is not a function
(anonymous function)
C:/Users/Denoh/wasilisha/Wasilisha_Africa/node_modules/react-redux/es/components/Provider.js:19
  16 |   };
  17 | }, [store]);
  18 | var previousState = useMemo(function () {
> 19 |   return store.getState();
     | ^  20 | }, [store]);
  21 | useEffect(function () {
  22 |   var subscription = contextValue.subscription;
View compiled
mountMemo
C:/Users/Denoh/wasilisha/Wasilisha_Africa/node_modules/react-dom/cjs/react-dom.development.js:15442
  15439 | function mountMemo(nextCreate, deps) {
  15440 |   var hook = mountWorkInProgressHook();
  15441 |   var nextDeps = deps === undefined ? null : deps;
> 15442 |   var nextValue = nextCreate();
  15443 |   hook.memoizedState = [nextValue, nextDeps];
  15444 |   return nextValue;
  15445 | }

please help me locate my error.请帮我找到我的错误。 when i try to use my redux store without the persist library, everything works fine.当我尝试在没有持久库的情况下使用我的 redux 商店时,一切正常。 but with the persist, i cant trace where my error is.但随着坚持,我无法追踪我的错误在哪里。

your store file exports a buildStore function, not store and persistor .您的store文件导出buildStore function,而不是storepersistor

import buildStore from "./store/";
import { PersistGate } from "redux-persist/integration/react";


const {store, persistor} = buildStore()

ReactDOM.render(

Always keep in mind that a file can have only one default export, so if you use the default import syntax for the same file for two different things, something is fishy;)永远记住,一个文件只能有一个默认导出,所以如果你对同一个文件使用默认的导入语法来处理两个不同的事情,有些事情是可疑的;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM