【问题标题】:action.type undefined in reduxaction.type 在 redux 中未定义
【发布时间】:2017-10-11 14:38:24
【问题描述】:

redux 出现错误:action.type 未定义。我已将其范围缩小到我的一项操作:

export function LOAD_PRODUCTS() {
    axios.get('http://localhost:3000/api/products')
    .then((res) => {
        return (dispatch) => {
            dispatch({type: 'PRODUCTS_LOADED', payload: res})
        }
    }) 
    .catch((err) => {
        return (dispatch) => {
            dispatch({type: "PRODUCTS_ERROR", payload: err})
        }
    })
}

或者也许是减速器...

export function reducer(
    state = {
        fbkey: '',
        userSuccess: null,
        cart: [],
        nav: null,
        tab: true,
        error: {}, 
        loading: false,
        products: []
    }, action) {


    switch (action.type) {
          case "FBTOKEN": {
            return {
                ...state,
                fbkey: action.payload
            }
          }

        case "NAV_OPEN" : {
            return {
                ...state, 
                nav: true
            }
        }

        case "NAV_CLOSE" : {
            return {
                ...state,
                nav: false
            }
        }

        case "USER_SUCCESS" : {
            return {
                ...state, 
                userSuccess: true
            }
        }

        case "USER_ERROR" : {
            return { 
                ...state,
                userSuccess: false
            }
        }

        case "ADD_TO_CART" : {
            return {
                ...state,
                cart: action.payload
            }
        }
        case "TAB_CLOSE": {
            return {
                ...state,
                tab: action.payload
            }
        }

        case "TAB_OPEN": {
            return {
                ...state, 
                tab: action.payload
            }
        }

        case "PRODUCTS_LOADED": {
            return {
                ...state,
                products: action.payload
            }
        }

        case "PRODUCTS_ERROR": {
            return {
                ...state, 
                error: action.payload
            }
        }

        case "IS_LOADING": {
            return {
                ...state, 
                loading: action.payload
            }
        }

        case "IS_NOT_LOADING": {
            return {
                ...state,
                loading: action.payload
            }
        }

        default: {
            return {
                ...state
            }
        }
    }
}

所以,不知何故,类型没有正确返回。我该如何解决?

编辑:这里正在使用 Thunk。我还添加了减速器代码。返回调度函数也不起作用。或许与此有关?

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    你需要返回 dispatch 试试下面的代码

    export function LOAD_PRODUCTS() {
        axios.get('http://localhost:3000/api/products')
        .then((res) => {
            return (dispatch) => {
                return dispatch({type: 'PRODUCTS_LOADED', payload: res})
            }
        }) 
        .catch((err) => {
            return (dispatch) => {
                return dispatch({type: "PRODUCTS_ERROR", payload: err})
            }
        })
    }
    

    【讨论】:

    • 我做到了。谢谢。
    • 只是出于好奇,有什么问题?而且它在没有返回调度员的情况下也能工作吗??
    • 不,它没有用。问题是 action.type 在那个地方是未定义的。 export default function routerMiddleware(history) { return function () { return function (next) { return function (action) { if (action.type !== CALL_HISTORY_METHOD) { return next(action); }
    • 如果您对答案感到好奇,reddit 就完成了reddit.com/r/reactjs/comments/75pl6n/actiontype_undefined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2021-08-05
    • 2016-12-22
    • 2018-03-15
    • 2015-12-24
    相关资源
    最近更新 更多