天天看点

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很容易找到原因。