簡體   English   中英

在 redux 中未定義 action.type

[英]action.type undefined in redux

我有一個 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。 我還添加了減速器代碼.. 返回調度函數也不起作用。 或許與此有關?

您需要返回調度嘗試下面的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM