我的代码工作正常,但每当我犯错编码并出现运行时错误时,我都会遇到烦人的问题。例如,在我的一个JSX页面中,我做了Date()
而不是new Date()
,而不是报告实际错误,我得到了......
Uncaught Error: Expected the reducer to be a function.
我做的任何错误几乎总是显示为此。它是从createStore.js
报告的,位于我的configureStore.jsx
代码中。
有没有办法让我能够获得更好的错误报告,帮助我找出真正的问题?非常感谢任何帮助或想法!!!
这是我的设置供参考....
main.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;
}
}
答案 0 :(得分:11)
更改以下行:
const nextRootReducer = require('../reducers/rootReducer');
要:
const nextRootReducer = require('../reducers/rootReducer').default;
答案 1 :(得分:4)
只要您想要导出该变量,请使用export const variable_name
代替const variable_name
。
对于ex:rootReducer.jsx应该重写为
import { combineReducers } from 'redux';
import { routerStateReducer } from 'redux-router';
import siteReducer from './siteReducer';
export const rootReducer = combineReducers({
router: routerStateReducer,
sites: siteReducer
});
export default rootReducer;
请注意带有const rootReducer的额外导出说明符
答案 2 :(得分:0)
我的问题是从根reducer路径导入Store而不是实际捆绑的商店根(窗口上的devtools和root reducer,中间件正在组合等)。
import Store from '../../../src/state/Store/reducer';
更改为
import Store from '../../../src/state/Store';