获取内部使用效果的值,而不会干扰事件序列
我在开始时具有以下使用效应,它将请求发送给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();
}, []);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,仅使用
async
/等待
:Here it is refactored to only use
async
/await
:如果您使用等待/async 键盘,则无需使用
。您可以做到这一点:
这里有更多信息:
If you use the await/async keywoards you don't need to also use the
.then()
function. You can do this instead:More infos here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#rewriting_a_promise_chain_with_an_async_function