我应该在reactjs中使用返回类型的动作调度吗?

问题描述 投票:0回答:1

我对使用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,
    };
}

现在我应该使用哪一个。请建议我。

reactjs redux store redux-thunk
1个回答
2
投票

如果您不需要执行异步操作,请使用此方法,

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


    }
}
© www.soinside.com 2019 - 2024. All rights reserved.