In react, one request depends on the result of another request.

The request in

react is usually placed in the lifecycle componentDidMount event, but now I have a requirement to send two requests, and the parameter of the second request is the result of the first request.

for example, the result of the first request: state.res1 if I put all the requests directly in the componentDidMount, the second request will not get parameters at first.

what I"m doing now is that the first request updates a boolean:state.res1IsCompleted logo in addition to updating the state.res1, puts the second request in the componentDidUpdate, and then determines whether or not to initiate the second request based on the res1IsCompleted logo.

but it"s a little weird that if my request relies too much (because I"m working on a workflow-related page, there will be a lot of design requests), there will be a bunch of identity status in state.

Mar.03,2022
Can't

be written in the callback of the request?


just put the second request in the callback function of the successful first request


js is asynchronous, so you can't get it.
I recommend two methods for you.

I don't see anything like redux in your description, so I assume that only the react, calling interface uses fetch .

  • callback

:<br>getProdDetail()<br>getRecListobjgoods_id,<br>export const getProdDetail = (obj) => {

return (dispatch) => {
    let method = 'b2c.product2.app_productBaseInfo&';
    const params = {
        appid: 'webapp',
        version: '3.9.2',
        source: 'wap',
        token: '',
        ...obj
    };
    const url = `${rootUrl}${method}` + combParams(params);
    axios.get(url).then((response) => {
        const res = response.data.data;
        dispatch(getProdInfo(res.returndata));
        const goodObj = {
            goods_id: res.returndata.goods_id,
            type: 1
        };
        getRecList(goodObj, dispatch)
    })
};

};

Menu