获取内部使用效果的值,而不会干扰事件序列

发布于 2025-02-08 16:05:50 字数 1161 浏览 3 评论 0 原文

我在开始时具有以下使用效应,它将请求发送给dever,然后在此当前实现中相应地修改状态,我想根据DO数据来操纵共享变量,但我不想将其余的逻辑放入响应块中由于数据立即显示,并且它会刹车我的旋转器逻辑,任何想法如何以正确的方式进行此事件,即使我在提取之前正在等待,目前共享也是不确定的吗?

useEffect(() => {
    const getData = async () => {
      let shared = '';
      await fetch('/encrypt', {
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
        method: 'POST',
        body: JSON.stringify({ clientId: params[1] }),
      })
        .then((res) => {
          res.json().then((data) => {
          
          shared = data.data.split('_Shared_')[1];
          });
        })
        .catch((err) => {
          dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
        });

      setLoading(true);
      dispatch(SetBackDrop(true));
      const docRef = doc(db, 'data-shared', shared);
      getDoc(docRef).then((docSnap) => {
        const data = docSnap.data();
        setLoaded(data?.img);
      });
      dispatch(SetBackDrop(false));
      setLoading(false);
    };
    getData();
  }, []);

I have the following useEffect at start it sends a request to the sever then modify the state accordingly in this current implementation I want to manipulate the shared variable according do data yet I don't want to put the rest of the logic inside the response block since the data show instantly and it brake my spinner logic any idea how to event this the right way , currently shared is undefined even if I am putting await before fetching ?

useEffect(() => {
    const getData = async () => {
      let shared = '';
      await fetch('/encrypt', {
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
        method: 'POST',
        body: JSON.stringify({ clientId: params[1] }),
      })
        .then((res) => {
          res.json().then((data) => {
          
          shared = data.data.split('_Shared_')[1];
          });
        })
        .catch((err) => {
          dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
        });

      setLoading(true);
      dispatch(SetBackDrop(true));
      const docRef = doc(db, 'data-shared', shared);
      getDoc(docRef).then((docSnap) => {
        const data = docSnap.data();
        setLoaded(data?.img);
      });
      dispatch(SetBackDrop(false));
      setLoading(false);
    };
    getData();
  }, []);

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

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

发布评论

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

评论(2

呆° 2025-02-15 16:05:50

在这里,仅使用 async /等待

useEffect(async () => {
    setLoading(true);
    const response = await fetch('/encrypt', {
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify({ clientId: params[1] }),
    })
    try {
      const data = await response.json();
    } catch(err) {
      dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
    }
    const shared = data.data.split('_Shared_')[1];
    dispatch(SetBackDrop(true));
    const docRef = doc(db, 'data-shared', shared);
    const docSnap = await getDoc(docRef);
    setLoaded(docSnap.data?.img);
    dispatch(SetBackDrop(false));
    setLoading(false);
});

Here it is refactored to only use async/await:

useEffect(async () => {
    setLoading(true);
    const response = await fetch('/encrypt', {
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify({ clientId: params[1] }),
    })
    try {
      const data = await response.json();
    } catch(err) {
      dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
    }
    const shared = data.data.split('_Shared_')[1];
    dispatch(SetBackDrop(true));
    const docRef = doc(db, 'data-shared', shared);
    const docSnap = await getDoc(docRef);
    setLoaded(docSnap.data?.img);
    dispatch(SetBackDrop(false));
    setLoading(false);
});
习ぎ惯性依靠 2025-02-15 16:05:50

如果您使用等待/async 键盘,则无需使用。您可以做到这一点:

const getData = async () => {
 try{
  const response = await fetch('/encrypt', {
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify({ clientId: params[1] }),
  });
  const result = await response.json();
  let shared = result.data.split('_Shared_')[1];
  //rest of the logic here
 }
 catch(e){
  dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
 }
}

这里有更多信息:

If you use the await/async keywoards you don't need to also use the .then() function. You can do this instead:

const getData = async () => {
 try{
  const response = await fetch('/encrypt', {
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify({ clientId: params[1] }),
  });
  const result = await response.json();
  let shared = result.data.split('_Shared_')[1];
  //rest of the logic here
 }
 catch(e){
  dispatch(SetSnackBarMsg({ bool: true, msg: 'Cannot get data.' }));
 }
}

More infos here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#rewriting_a_promise_chain_with_an_async_function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文