React JS SetState在嵌套Axios柱中不起作用

发布于 2025-02-05 08:23:03 字数 976 浏览 2 评论 0原文

我正在尝试从嵌套的axios.post调用访问res.data.id,并将其分配给'activeId'变量。我在按钮点击事件上调用handlesaveall()函数。单击按钮时,当我安装“ res.data.id”时,它正确返回值,但是当我安装'activeId'时,它返回null,这意味着无法分配“ res.data.id” 。 我只需要将“ res.data.id”的值分配给'metricid',这样我就可以在其他函数(例如save2()function的其他函数中使用它。 有人有解决方案吗?提前致谢

const [activeId, setActiveId] = useState(null);

useEffect(() => {}, [activeId]);

const save1 = () => {
      axios.get(api1, getDefaultHeaders())
        .then(() => {
          const data = {item1: item1,};

          axios.post(api2, data, getDefaultHeaders()).then((res) => {
            setActiveId(res.data.id);
            console.log(res.data.id); // result: e.g. 10
          });
      });
};

const save2 = () => {
  console.log(activeId); // result: null
};

const handleSaveAll = () => {
  save1();
  save2();

  console.log(activeId); // result: again its still null
};

return (
  <button type='submit' onClick={handleSaveAll}>Save</button>
);

I am trying to access the res.data.id from a nested axios.post call and assign it to 'activeId' variable. I am calling the handleSaveAll() function on a button Click event. When the button is clicked, When I console the 'res.data.Id', its returning the value properly, but when I console the 'activeId', it's returning null, which means the 'res.data.id' cannot be assigned.
I just need to assign the value from 'res.data.id' to 'metricId' so that I can use it somewhere else in another function like save2() function.
Does anyone have a solution? Thanks in advance

const [activeId, setActiveId] = useState(null);

useEffect(() => {}, [activeId]);

const save1 = () => {
      axios.get(api1, getDefaultHeaders())
        .then(() => {
          const data = {item1: item1,};

          axios.post(api2, data, getDefaultHeaders()).then((res) => {
            setActiveId(res.data.id);
            console.log(res.data.id); // result: e.g. 10
          });
      });
};

const save2 = () => {
  console.log(activeId); // result: null
};

const handleSaveAll = () => {
  save1();
  save2();

  console.log(activeId); // result: again its still null
};

return (
  <button type='submit' onClick={handleSaveAll}>Save</button>
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-02-12 08:23:03

代码运行同步的这一部分

const handleSaveAll = () => {
  save1();
  save2();

  console.log(activeId); // result: again its still null
};

,但是在那里您

axios.get(api1, getDefaultHeaders())
        .then(() => {

可以将代码重构为异步/等待这样的代码:

const save1 = async () => {
      const response = await axios.get(api1, getDefaultHeaders());
      const response2 = await axios.post(api2, { item1: response.data.item1 }, getDefaultHeaders());
      return response2.data.id;
};

const save2 = (activeId) => {
  console.log(activeId); // result: null
};

const handleSaveAll = async () => {
  const activeId = await save1();
  save2(activeId);
  setActiveId(activeId);
  console.log(activeId); // result: again its still null
};

或这样的承诺链,如下:


const save2 = (activeId) => {
  console.log(activeId); // result: null
};

const save1 = () => {
  return axios.get(api1, getDefaultHeaders())
    .then(({ data }) => {
      const data = {item1: item1,};
      return axios.post(api2, {item1: data.item1}, getDefaultHeaders())
    })
    .then((res) => res.data.id);
};

const handleSaveAll = () => {
  save1()
    .then((res) => {
      setActiveId(res.data.id);
      console.log(res.data.id); // result: e.g. 10
      return res.data.id;
    })
    .then(save2);
};

This part of code run sync

const handleSaveAll = () => {
  save1();
  save2();

  console.log(activeId); // result: again its still null
};

but there you run async

axios.get(api1, getDefaultHeaders())
        .then(() => {

You can refactor your code to async/await like this:

const save1 = async () => {
      const response = await axios.get(api1, getDefaultHeaders());
      const response2 = await axios.post(api2, { item1: response.data.item1 }, getDefaultHeaders());
      return response2.data.id;
};

const save2 = (activeId) => {
  console.log(activeId); // result: null
};

const handleSaveAll = async () => {
  const activeId = await save1();
  save2(activeId);
  setActiveId(activeId);
  console.log(activeId); // result: again its still null
};

or to chain of promises, like this:


const save2 = (activeId) => {
  console.log(activeId); // result: null
};

const save1 = () => {
  return axios.get(api1, getDefaultHeaders())
    .then(({ data }) => {
      const data = {item1: item1,};
      return axios.post(api2, {item1: data.item1}, getDefaultHeaders())
    })
    .then((res) => res.data.id);
};

const handleSaveAll = () => {
  save1()
    .then((res) => {
      setActiveId(res.data.id);
      console.log(res.data.id); // result: e.g. 10
      return res.data.id;
    })
    .then(save2);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文