javascript - react Redux 未捕获错误 : Expected the reducer to be a function

标签 javascript reactjs redux react-redux

我正在学习 React,在尝试使用 Redux 设置 React 时遇到了问题。我在使用 createStore 时遇到此错误:

Uncaught Error: Expected the reducer to be a function

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;
    }
}

我见过其他类似的问题,但似乎都没有解决我的问题。

最佳答案

如果你想导入你正在做的方式,你需要导出default:

export default (state = {}, action) => {
    switch (action.type){
        case 'GET_MOVIE_RECEIVED':
            return action.data;
        default:
            return state;
    }
}

否则,您可以这样导入:

import { movieReducer } from './reducers';

关于javascript - react Redux 未捕获错误 : Expected the reducer to be a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141335/

相关文章:

javascript - JavaScript 中的 MVC : model returns undefined

javascript - findAndModify,如何对文档的数组搜索对象进行操作并更改字段值

带有 ajax 的 Javascript 回调函数

javascript - react / Hook : prevent re-rendering component with a function as prop

javascript - 如何添加Firebase实时数据库的删除数据功能

reactjs - React Redux 处理 DOM 元素启用/禁用的方式

javascript - 如何从javascript数组打印空值?

javascript - 具有多个变量的 React JS 单选按钮组

javascript - react-redux-react-router v4 url​​,更新但组件不更新

react-native - TextInput 通过引用获取值 react native