简体   繁体   中英

Store does not have a valid reducer when using redux-toolkit

I'm having some trouble with redux-toolkit configureStore function, whenever I try to call dispatch on a page it gives me the 'Store does not have a valid reducer' error. I suppose my imports are right and also the store is in the upper level of my index.js file.

This is how it's looking:

store.js file:

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

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

book.js file:

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 file:

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')
);

Thanks in advance!

Actually it was my mistake, there was two stores (one from the template). I was importing the original one instead of the one I created.

I think if you change the configureStore to this:

export default configureStore({
  reducer: bookReducer
});

It should work.

I think that you should change the import name in the store.js file

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

export default configureStore({
  reducer: { books: BookReducer } // bookReducer -> BookReducer
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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