gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:45:26 24 4
gpt4 key购买 nike

我正在学习 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/

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