gpt4 book ai didi

javascript - React + Redux-router = Uncaught Error : Expected the reducer to be a function

转载 作者:行者123 更新时间:2023-12-03 13:09:53 25 4
gpt4 key购买 nike

我的代码工作正常,但每当我犯编码错误并出现运行时错误时,我都会遇到一个恼人的问题。例如,在我的一个 JSX 页面中,我执行了 Date() 而不是 new Date(),并且没有报告实际错误,而是......

Uncaught Error: Expected the reducer to be a function.

我犯的任何错误几乎总是显示为这样。这是从 createStore.js 报告的,位于下面的 configureStore.jsx 代码中。

有没有一种方法可以获得更好的错误报告来帮助我识别真正的问题?非常感谢任何帮助或想法!!!

这是我的设置供引用......

ma​​in.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ReduxRouter } from 'redux-router';
import configureStore from './store/configureStore'
import routes from './routes';

const rootEl = document.getElementById('app-container');

const store = configureStore();

ReactDOM.render(
<div>
<Provider store={store}>
<ReduxRouter routes={routes} />
</Provider>
</div>
, rootEl
);

configureStore.jsx

import { createHashHistory } from 'history';
import { applyMiddleware, createStore, compose } from 'redux';
import { reduxReactRouter } from 'redux-router';

import thunk from 'redux-thunk';
import promiseMiddleware from 'redux-promise-middleware';

import rootReducer from '../reducers/rootReducer';
import routes from '../routes';

export default function configureStore(initialState = {}) {

const history = createHashHistory();

const middlewares = [
thunk,
promiseMiddleware({
promiseTypeSuffixes: ['PENDING','SUCCESS','ERROR']
})
];

const toolChain = [
applyMiddleware(...middlewares),
reduxReactRouter({
routes,
history
})
];

const store = compose(...toolChain)(createStore)(rootReducer, initialState);

if (module.hot) {
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers/rootReducer');
store.replaceReducer(nextRootReducer);
});
}
return store;
}

rootReducer.jsx

import { combineReducers } from 'redux';
import { routerStateReducer } from 'redux-router';
import siteReducer from './siteReducer';

const rootReducer = combineReducers({
router: routerStateReducer,
sites: siteReducer
});
export default rootReducer;

siteReducer.jsx

import {GET_SITES} from '../actions/siteActions';

const defaultState = {
isPending: null,
isSuccess: null,
isError: null,
error: null,
data: null
};

export default function siteReducer(state = defaultState, action) {

switch (action.type) {
case `${GET_SITES}_PENDING`:
return {
...defaultState,
isPending: true
};
case `${GET_SITES}_SUCCESS`:
return {
...defaultState,
isSuccess: true,
error: false,
data: action.payload
};
case `${GET_SITES}_ERROR`:
return {
...defaultState,
isError: true,
error: action.payload
};
default:
return state;
}
}

最佳答案

更改以下行:

const nextRootReducer = require('../reducers/rootReducer');

致:

const nextRootReducer = require('../reducers/rootReducer').default;

关于javascript - React + Redux-router = Uncaught Error : Expected the reducer to be a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com