useState 不更新状态

发布于 2025-01-11 00:32:45 字数 868 浏览 0 评论 0原文

我在下面的代码中更改了 isDeleting 的状态。此状态用于显示删除按钮上的微调器。删除任务完成后,此状态将更改为 false

const { deleteExpense } = apiExpenses();
const [isDeleting, setIsDeleting] = useState(false)


const handleDeleteCompleted =  () => {
    setIsDeleting(true)
    
    const response = deleteExpense(fetchExpenseId);

    if(response) {
      setOpenConfirmDelete(false);
      setIsDeleting(false)
    }
};

\\in JSX

<ButtonError
onClick={handleDeleteCompleted}
size="large"
sx={{
  mx: 1,
  px: 3,
}}
variant="contained"
startIcon={
  isDeleting ? <CircularProgress size="1rem" /> : null
}
disabled={isDeleting}
>
{t("Delete")}
</ButtonError>

handleDeleteCompleted 函数运行时,它不会更改 isDeleting 的状态,并且如果我始终对其进行控制台保持false。它还运行 deleteExpense 函数。

因此,旋转器永远不会弹出。

这里出了什么问题?

I have below code where I change state of isDeleting. This state is used to show the spinner on the delete button. Once the delete task is finished this state is changed to false

const { deleteExpense } = apiExpenses();
const [isDeleting, setIsDeleting] = useState(false)


const handleDeleteCompleted =  () => {
    setIsDeleting(true)
    
    const response = deleteExpense(fetchExpenseId);

    if(response) {
      setOpenConfirmDelete(false);
      setIsDeleting(false)
    }
};

\\in JSX

<ButtonError
onClick={handleDeleteCompleted}
size="large"
sx={{
  mx: 1,
  px: 3,
}}
variant="contained"
startIcon={
  isDeleting ? <CircularProgress size="1rem" /> : null
}
disabled={isDeleting}
>
{t("Delete")}
</ButtonError>

When handleDeleteCompleted function runs it does not change state of isDeleting and if I console it always stay in false. It runs deleteExpense function as well.

and for that reason, the spinner never popup.

What is getting wrong here?

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

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

发布评论

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

评论(1

活雷疯 2025-01-18 00:32:45

deleteExpense() 应该是一个 async 函数吗?在您当前的示例中,您将其视为同步函数。这导致设置 setIsDeleting(true)setIsDeleting(false) 之间没有时间,因为 React 在这些操作同步发生时将其捆绑在一起,后者会覆盖前者,并且您可以从未真正调用setIsDeleting(true)

要解决此问题,请尝试使您的函数异步

const handleDeleteCompleted = async() => {
    setIsDeleting(true)
    
    const response = await deleteExpense(fetchExpenseId);

    if(response) {
      setOpenConfirmDelete(false);
      setIsDeleting(false)
    }
};

Should deleteExpense() be an async function? In your current example you're treating it as a synchronous function. This results in that there is no time between setting setIsDeleting(true) and setIsDeleting(false) because React bundles these actions when they happen synchronously, the latter overrides the former and you never actually invoke setIsDeleting(true).

To fix this, try making your function async

const handleDeleteCompleted = async() => {
    setIsDeleting(true)
    
    const response = await deleteExpense(fetchExpenseId);

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