Redux 期望 reducer 是一个函数
Posted
技术标签:
【中文标题】Redux 期望 reducer 是一个函数【英文标题】:Redux expecting the reducer to be a function 【发布时间】:2018-10-18 21:29:04 【问题描述】:Redux 把我扔了:
错误:预计减速器是一个函数。 在行
来自索引:
const store = configureStore();
来自 configureStore:
const store = createStore(rootReducer, initialState, devToolsEnhancer())
index.js
import React from 'react';
import render from 'react-dom';
import Root from './Root';
import './index.css';
import App from './whitesheet-components/App';
import registerServiceWorker from './registerServiceWorker';
import configureStore from './store/configureStore';
const store = configureStore();
const MOUNT_NODE = document.getElementById('root');
const load = Component =>
render(
<App>
<Component store=store />
</App>,
MOUNT_NODE,
);
load(Root);
registerServiceWorker();
configureStore.js
import createStore from 'redux';
import devToolsEnhancer from 'redux-devtools-extension';
import rootReducer from './rootReducer';
const isDevelopment = process.env.NODE_ENV !== 'production';
export default function configureStore(initialState = )
const store = createStore(rootReducer, initialState, devToolsEnhancer());
if (isDevelopment && module.hot)
// Enable Webpack hot module replacement for reducers
module.hot.accept('./rootReducer.js', () =>
const nextReducer = require('./rootReducer').default; // eslint-disable-line global-require
store.replaceReducer(nextReducer);
);
return store ;
rootReducer
import mockReducer from './mockReducer';
export default mockReducer ;
mockReducer
export default function reducer(state = hola: 'as' , type )
switch (type)
default:
return state;
找不到错误。第一次自己创建一个支持 devtools 插件的 redux 商店
【问题讨论】:
【参考方案1】:看起来您正在导出一个对象而不是根减速器中的一个函数。
rootReducer
import mockReducer from './mockReducer';
// export default mockReducer ;
export default mockReducer;
【讨论】:
谢谢!那行得通。如果我不能将多个减速器包装在一个对象中,如何导出它们? @victor.ja 我建议每个 reducer 使用不同的文件 你可以export default mockReducer, secondReducer
导入时做import mockReducer, secondReducer from './mockReducer'
以上是关于Redux 期望 reducer 是一个函数的主要内容,如果未能解决你的问题,请参考以下文章