首页 > 解决方案 > Redux-toolkit:Store 没有有效的 reducer。确保传递给 combineReducers 的参数是一个其值为 reducers 的对象

问题描述

我不知道为什么它向我显示一个错误我什至没有开始编码但我只是编写了文档给出的 configureStore 语法

我通过提供者上传到 index.js 文件

这是我的应用程序 .js

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>Redux Tool Kit</h1>
    </div>
  );
}

export default App;

这是我的商店,js

import { configureStore } from '@reduxjs/toolkit'
const store = configureStore({
  reducer: {},
})
export default store;

这是我的 index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import {Provider} from 'react-redux'
import  store  from './store/store'
import App from './App';
import reportWebVitals from './reportWebVitals';

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

// If you want to start measuring performance in your app, pass a function


结果是错误存储没有有效的减速器。确保传递给 combineReducers 的参数是一个其值为 reducers 的对象。

标签: reactjsreact-reduxredux-toolkit

解决方案


你还没有在你的应用程序中定义任何 reducer 函数

import { configureStore,createSlie } from '@reduxjs/toolkit';
const reducerSlice = createSlice({
  name: 'store',
  initialState: {},
  reducers: {
     someAction: function() {

     }
  }
})
const store = configureStore({
  reducer: {
    someReducer: reducerSlice.reducer,
  }
})
export default store;

您还可以slices/reducers在您的应用程序中创建多个并使用combineReducers并将其传递给 configureStore 中的 reducer 对象

注意:combineReducer也可作为从 @reduxjs/toolkit


推荐阅读