使用 redux-toolkit 时 Store 没有有效的 reducer
Store does not have a valid reducer when using redux-toolkit
我在使用 redux-toolkit configureStore 函数时遇到了一些问题,每当我尝试在页面上调用 dispatch 时,它都会给我 'Store does not have a valid 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')
);
提前致谢!
我想如果你把 configureStore
改成这样:
export default configureStore({
reducer: bookReducer
});
应该可以。
实际上是我的错误,有两家商店(一家来自模板)。
我正在导入原始的而不是我创建的。
我在使用 redux-toolkit configureStore 函数时遇到了一些问题,每当我尝试在页面上调用 dispatch 时,它都会给我 'Store does not have a valid 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')
);
提前致谢!
我想如果你把 configureStore
改成这样:
export default configureStore({
reducer: bookReducer
});
应该可以。
实际上是我的错误,有两家商店(一家来自模板)。 我正在导入原始的而不是我创建的。