1. 程式人生 > 其它 >redux Actions may not have an undefined “type“ property. Have you misspelled a constant?

redux Actions may not have an undefined “type“ property. Have you misspelled a constant?

react-redux 報錯

Actions may not have an undefined "type" property. Have you misspelled a constant?

出現類似的錯誤,很大機率是因為redux 操作action動作時候的type 名稱問題

檢查action 中的type 是否和reducer 操作中的type 名稱是一致

兩處用到的 type 需要一致

//./action.js 
// 型別為type:types.USER
export const userAction=(data)=>({type:"型別名稱",data});

// ./reducer.js
let token =null;

export default function Token(_token=token,action){
    //解構中的type用來做判斷條件
    const {type,data}=action;
    switch(type){
        case "判斷條件":
        return data;
        default:
        return _token;    
    }
    
}