【问题标题】:TypeError: store.getState is not a function (redux)TypeError:store.getState 不是函数(redux)
【发布时间】:2021-08-16 08:08:46
【问题描述】:

我不断收到此错误:

TypeError: store.getState 不是函数

我认为问题可能出在我的 store.js 中,但我不确定在哪里。

以下是代码:

root reducer.js

import { combineReducers } from "redux";

import userReducer from "./User/userReducer";

export default combineReducers({
  user: userReducer,
});

root saga.js

import { all, call } from "redux-saga/effects";

import userSaga from "./User/user.saga";

//generator function
export default function* rootSaga() {
  //to pass our sagas
  yield all([call(userSaga)]);
}

store.js

import { createStore, applyMiddleware } from "redux";
import logger from "redux-logger";
import thunk from "redux-thunk";
import createSagaMiddleWare from "redux-saga";
import { persistStore } from "redux-persist";

import rootReducer from "./rootReducer";
import rootSaga from "./rootSaga";

const sagaMiddleware = createSagaMiddleWare();
export const middleWares = [thunk, sagaMiddleware, logger];

export const store = createStore(rootReducer, applyMiddleware(...middleWares));
sagaMiddleware.run(rootSaga);

//----
export const persist = persistStore(store);

export default { store, persist };

index.js

import { BrowserRouter } from "react-router-dom";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "react-redux";
import store from "./Redux/store";

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <BrowserRouter>
        <App />
      </BrowserRouter>
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

【问题讨论】:

  • 您使用导出对象类型导出存储,因此您必须像这样导入它:import {store} from 'path/to/store'。如果你像这样导出存储:导出默认存储,所以你可以像这样导入它:从'path/to/store'导入存储

标签: javascript reactjs redux react-redux redux-saga


【解决方案1】:

你以错误的方式导入store

import { store } from "./Redux/store";

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2018-03-17
    • 2015-12-29
    • 1970-01-01
    • 2020-10-26
    • 2022-01-06
    • 2017-12-30
    • 2020-08-18
    • 2017-03-14
    相关资源
    最近更新 更多