简体   繁体   English

这个错误信息有什么用? 动作必须是普通对象。 使用自定义中间件进行异步操作

[英]what is this error message for ? Actions must be plain objects. Use custom middleware for async actions

actionTypes.js actionTypes.js

export const ADD_INGREDIENT = 'ADD_INGREDIENT';
export const REMOVE_INGREDIENT = 'REMOVE_INGREDIENT';
export const SET_INGREDIENTS = 'SET_INGREDIENTS';
export const FETCH_INGREDIENTS_FAILED = 'FETCH_INGREDIENTS_FAILED';

burgerBuilder.js /actions burgerBuilder.js /动作

export const setIngredients = (ingredients) => {
    return {
        type: actionTypes.SET_INGREDIENTS,
        ingredients: ingredients
    };
};

export const fetchIngredientsFailed = () => {
    return {
        type: actionTypes.FETCH_INGREDIENTS_FAILED
    };
};

export const initIngredients = () => {
    return dispatch => {
        axios.get('/ingredients.json')
            .then(response => {
                dispatch(setIngredients(response.data))
            })
            .catch(error => {
                dispatch(fetchIngredientsFailed())
            });
    };
};

burgerBuilder.js /reducer burgerBuilder.js /reducer

case actionTypes.SET_INGREDIENTS:
     return {
         ...state,
         ingredients: action.ingredients,
         error: false
     };
case actionTypes.FETCH_INGREDIENTS_FAILED:
     return {
         ...state,
         error: true
     };

BurgerBuilder.js/containers BurgerBuilder.js/容器

componentDidMount () {
    this.props.onInitIngredients();
}


//Some Code...


const mapStateToProps = state => {
    return {
        ings: state.ingredients,
        price: state.totalPrice,
        error: state.error
    }
};

const mapDispatchToProps = dispatch => {
    return {
        onIngredientAdded: (ingName) => dispatch(burgerbuilderActions.addIngredient(ingName)),
        onIngredientRemoved: (ingName) => dispatch(burgerbuilderActions.removeIngredient(ingName)),
        onInitIngredients: () => dispatch(burgerbuilderActions.initIngredients())
    }
};

export default connect(mapStateToProps, mapDispatchToProps)(withErrorHandler(BurgerBuilder, axios));

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware, compose} from 'redux';
import {BrowserRouter} from 'react-router-dom';
import thunk from 'redux-thunk';
import burgerBuilderReducer from './store/reducers/burgerBuilder';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION__ || compose;

const store = createStore(burgerBuilderReducer, composeEnhancers(
    applyMiddleware(thunk)
));

const app = (
    <Provider store={store}>
        <BrowserRouter>
            <App/>
        </BrowserRouter>
    </Provider>
);

I've got this error "Actions must be plain objects. Use custom middleware for async actions" when I was working on a demo project.我在处理演示项目时遇到此错误“操作必须是普通对象。使用自定义中间件进行异步操作”。 I don't know but I feel that this error comes from initIngredients() in burgerBuilder.js/action file.我不知道,但我觉得这个错误来自 burgerBuilder.js/action 文件中的 initIngredients() 。 I'm new in react!我是新来的反应!

It seems that the code is currently broken, you can fix this issue by change:似乎代码当前已损坏,您可以通过更改来解决此问题:

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION__ || compose;

const store = createStore(burgerBuilderReducer, composeEnhancers(
    applyMiddleware(thunk)
));

with:和:

const store = createStore(
  rootReducer,
  //composeEnhancers(applyMiddleware(thunk)) // => NOTE: This would break the code!
  // Thisone instead will work fine...
  compose(
    applyMiddleware(
      thunk
      // Other middlewares
    ),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() // Chrome debugger
  )
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异步操作 Redux 未处理拒绝(错误):操作必须是普通对象。 使用自定义中间件进行异步操作 - Async Action Redux Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions 错误服务器错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - Error server Error: Actions must be plain objects. Use custom middleware for async actions 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error 错误 - 操作必须是普通对象。 使用自定义中间件进行异步操作 - Error - Actions must be plain objects. Use custom middleware for async actions React-Redux:动作必须是普通对象。 使用自定义中间件进行异步操作错误 - React-Redux: Actions must be plain objects. Use custom middleware for async actions Error react-redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - react-redux Error: Actions must be plain objects. Use custom middleware for async actions React Native 和 Redux - 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - React Native & Redux - Error: Actions must be plain objects. Use custom middleware for async actions 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.? Typescript、React 和 Redux axios 错误 - 操作必须是普通对象。 使用自定义中间件进行异步操作 - Typescript, React and Redux axios Error - actions must be plain objects. use custom middleware for async actions CreateAsyncThunk 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - CreateAsyncThunk Error: Actions must be plain objects. Use custom middleware for async actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM