天天看點

react 項目——踩坑日記(1)

背景:react項目,引入redux,axios。

問題:

   axios請求json對象成功=>進行到then語句中,依次執行=>執行到then 裡面的dispatch(action)時,并不執行=》直接執行catch()裡面的内容。

例如如下代碼:

export const getActiveSearchInfo=(month,week)=>{
    return (dispatch)=>{
            
            axios.get('/api/ActiveData_1_1.json').then((res)=>{
            const result=res.data;
            const action=getActiveInfo(result.data);
            console.log(action);
            //下面這個dispatch不執行
            dispatch(action);
            //下面一行也不會執行
            console.log('654');
            }).catch(()=>{
            //從dispatch那裡直接開始執行這裡
            console.log('123');
            dispatch(pageNotFound());
        })
    }
};
           

解決:

 這裡的錯誤其實是reducer裡面的錯誤造成的,但是錯誤确是在dispatch處顯示,導緻直接執行catch()語句。

踩坑心得:

 這種錯誤一般不好找到,具體的錯誤也五花八門。可以在catch(error)裡加上error,列印error很容易找到原因。