【问题标题】:Store does not have a valid reducer when using redux-toolkit使用 redux-toolkit 时商店没有有效的 reducer
【发布时间】:2020-06-18 16:37:52
【问题描述】:

我在使用 redux-toolkit configureStore 函数时遇到了一些问题,每当我尝试在页面上调用 dispatch 时,都会出现“Store 没有有效的 reducer”错误。我想我的导入是正确的,并且商店也在我的 index.js 文件的上层。

这就是它的样子:

store.js 文件:

import { configureStore } from "@reduxjs/toolkit";
import bookReducer from "../reducers/book";

export default configureStore({
  reducer: { books: bookReducer }
});

book.js 文件:

import { createSlice } from '@reduxjs/toolkit'

export const bookSlice = createSlice({
  name: 'books',
  initialState: {
    bookList: []
  },
  reducers: {
    getBookSuccess: (state, action) => console.log(action.payload),
    getBookFailure: (state, action) => console.log(action.payload),
    setLoading: (state, action) => ({ ...state, loadingTarget: action.loadingTarget, loadingType: action.loadingType })
  }
})

export const { getBookSuccess, getBookFailure, setLoading } = bookSlice.actions;

export default bookSlice.reducer;

index.js 文件:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss';
import App from './App';
import store from './app/store';
import { Provider } from 'react-redux';
import './fontAwesome'

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

提前致谢!

【问题讨论】:

  • 看起来有效。你能记录一下booksReducer 在你的store.js 文件中的内容吗?它实际上是一个函数吗?
  • 哦,您的评论表明,甚至日志记录都不起作用,导入来自另一个文件夹中的另一个商店。我的错!还是谢谢!!

标签: javascript reactjs redux redux-toolkit


【解决方案1】:

其实是我的错,有两家商店(一家来自模板)。 我导入的是原始的,而不是我创建的。

【讨论】:

    【解决方案2】:

    我想如果你把configureStore改成这样:

    export default configureStore({
      reducer: bookReducer
    });
    

    它应该可以工作。

    【讨论】:

    • 不幸的是它不起作用,在文档中,如果我传递一个减速器对象,它会调用组合减速器函数。所以它应该工作......
    • @Rodolfo 很抱歉没有帮助您解决问题,感谢您指出我不知道的 redux-toolkit 功能。
    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2020-04-24
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多