我正在学习React并且在尝试使用Redux设置React时遇到了问题。我在createStore上遇到了这个错误:
未捕获错误:期望reducer成为函数
index.js
import React from 'react';
import {render} from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware, compose} from 'redux';
import movieReducer from './reducers';
import App from './App';
import dataService from './services/dataService';
let store = createStore(movieReducer, {}, applyMiddleware(dataService));
render(
<Provider store={store}>
<App/>
</Provider>, document.getElementById('main')
)
reducers.js
export const movieReducer = (state = {}, action) => {
switch (action.type){
case 'GET_MOVIE_RECEIVED':
return action.data;
default:
return state;
}
}
我已经看到过这样的其他问题,但似乎没有一个能解决我的问题。
答案 0 :(得分:3)
如果您想以您的方式导入,则需要导出default
:
export default (state = {}, action) => {
switch (action.type){
case 'GET_MOVIE_RECEIVED':
return action.data;
default:
return state;
}
}
否则,您可以像这样导入:
import { movieReducer } from './reducers';