在 Reactjs Redux Redux-thunk 中进行依赖 api 调用的正确方法是什么

问题描述:

我想在第一次加载时在一个组件中显示数据.我有一个 API 调用,第二个 API 取决于第一个 API 响应.如何管理此调用序列和错误处理?如果有人有示例链接,那对我来说非常好,因为我是 ReactjsRedux 的新手.

I want to show data in one component at the first load time. I have an API call and the second API is depending on the first API response. How can I manage this call sequence and error handling? If anyone has an example link it would we very good for me because I am new in Reactjs and Redux.

我有一个例子.我进行了 2 个 API 调用.我需要注册一个用户(第一个请求)和他的伙伴.为了注册合作伙伴(第二个请求),我需要从 first request response 中获取 token 并在第二个将其作为 header 传递请求:

I have an example. I make 2 API calls. I need to register a user (first request) and his partner. In order to register the partner (second request), I need to take the token from first request response and pass it as a header at second request:

注册函数:

 const register = () => {

    const data = {
      eMail: email,
      password: password,
    };

    const partnerData = {
      firstName: firstName,
      lastName: lastName,
    };

    axios
      .post("/register", data)
      .then((firstResponse) => {
       //do sth
 
          return axios
            .post("/registerPartner", partnerData, {
              headers: { Token: firstResponse.data.token },
            })
            .then((secondResponse) => {
              history.push("/home");
              window.location.reload();
              return secondResponse.data;
            })
            .catch((error) => {
              console.log("Error in second request");
            });
       
      })
      .catch((error) => {
        console.log("Error in first request");
      });
  };