简体   繁体   English

在 React 中删除方法后如何更新我的组件?

[英]How can i update my component after delete methods in React?

I use Sweetalert2 and react(tsx) and i don't know how to refresh my item list after delete methods.我使用 Sweetalert2 和 react(tsx),我不知道如何在删除方法后刷新我的项目列表。

Here is the code (i have a button that use this functions):这是代码(我有一个使用此功能的按钮):

function DeleteCard(item: DataItem){
    Swal.fire({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
      if (result.isConfirmed) {
        const response = await Api.delete('api/stuff/${item.Id}')
        Swal.fire(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        )
      }
    }).then(function(){
        window.location.reload(); // this is page reload, it is woking but not a good solutions
    })
}

I tried window.location.reload();, but not a good solution.我尝试了 window.location.reload();,但不是一个好的解决方案。

If you receive your items list from an api , instead of window.location.reload() , fetch the item list again.如果您从api而不是window.location.reload()收到项目列表,请再次获取项目列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新我的 React 组件 state? - How can I update my React component state? 在将新项目添加到数据库后,如何使用 React 钩子更新 React 中的组件? - How can I update component in React after adding a new item into the database, using react hooks? 如何告诉我的React app我想在Firestore中更新哪个组件? - How can I tell my React app which component I want to update in Firestore? React Native - 如何在三元 function 之后更新我的组件? - React Native - How to update my component after ternary function? 为什么我无法更新我的反应组件的状态? - Why I can't get update the state of my react component? 我如何通过RxJS限制对React组件方法的调用 - How can I throttle calls of methods of a react component via RxJS 单击句柄提交后如何更新组件数据 - How can I update component data after click handle submit in react 如何在集成测试中防止未安装组件的 React 状态更新? - How can I prevent a React state update on an unmounted component in my integration testing? 如何在 React 中从父 class 更新我的子组件 state? - How can I update my child component state from the parent class in React? 如何在React中从其子组件之一更新容器组件的状态? - How can I update the state of my container component from one of its children in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM