我对使用dispatch感到困惑。请看下面的代码。
export function getUserInfo(isDeviceinfo) {
return (dispatch) => {
dispatch({
type: REQUEST_DEVICE_MODEL_RESET,
isDeviceinfo,
});
};
}
要么
export function getUserInfo(isDeviceinfo) {
return => {
type: REQUEST_DEVICE_MODEL_RESET,
isDeviceinfo,
};
}
现在我应该使用哪一个。请建议我。
如果您不需要执行异步操作,请使用此方法,
export function getUserInfo(isDeviceinfo) {
return{
type: REQUEST_DEVICE_MODEL_RESET,
isDeviceinfo,
};
}
如果需要执行异步操作,请使用dispatch。
function getUserInfo(isDeviceinfo) {
return (dispatch)=>{
//perform a async operation like this http call
return fetch(SOME_URL).then(j=>j.json()).then((d)=>{
dispatch({
type: REQUEST_DEVICE_MODEL_RESET,
isDeviceinfo,
})
})
}
}