You need to enable JavaScript to run this app. React整合Redux时Store不存在有效reducer报错问题求解
优惠活动
大模型
产品
解决方案
定价
更多

React整合Redux时Store不存在有效reducer报错问题求解

React+Redux项目运行报"Store does not have a valid reducer"的解决方法

问题场景

首次尝试在React项目中搭配Redux开发,在Edge浏览器中运行时报错:

Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.

排查多篇相关内容均未找到匹配场景的解决方案,初始代码如下:

初始store.js代码

import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'

const reducer = combineReducers({})

const initialState = {}
 
const middleware = [thunk]

const store = createStore(reducer, initialState,
      composeWithDevTools(applyMiddleware(...middleware)))

export default store

初始index.js代码

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

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

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more.
reportWebVitals();

问题根因

combineReducers方法要求传入的对象必须至少包含一个合法的reducer函数作为属性值,初始代码中给combineReducers传入了空对象,触发Redux的参数校验报错。

修复步骤

  1. 新增/reducers/productReducer.js文件,编写reducer业务逻辑:
export const productListReducer = (state = { products: [] }, action ) => {
    switch(action.type) {
        case 'PRODUCT_LIST_REQUEST':
            return {loading:true, products:[]};
        case 'PRODUCT_LIST_SUCCESS':            
            return {loading:false, products:action.payload};
        case 'PRODUCT_LIST_FAIL':
            return {loading:false, error:action.payload};
        default:
            return state
    }
}
  1. 修改store.js,引入编写好的reducer传入combineReducers:
import { productListReducer } from './reducers/productReducers';

const reducer = combineReducers({
    productList: productListReducer,
})

内容的提问来源于stack exchange,提问作者jeremy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 04:09:04