【问题标题】:I would like to pop up confirmation message before deleting the data react and django?我想在删除数据之前弹出确认消息 react 和 django?
【发布时间】:2022-07-21 14:06:08
【问题描述】:

我可以在没有确认消息的情况下删除数据,请帮我在 Axios 部分之间设置确认。与此一起添加的代码。

const navigate = useNavigate();
    const Delete = async () => {

        await axios({
            method: 'DELETE',
            url: `http://localhost:8000/update/${id}`,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
            

        }).then(response => {
            navigate('/')
        })
    }

【问题讨论】:

标签: reactjs


【解决方案1】:

如果您想在删除前确认,请按此操作


import { confirmAlert } from 'react-confirm-alert'; // Import
import 'react-confirm-alert/src/react-confirm-alert.css'; // Import css

function App() {

    const Delete = async () => {

        await axios({
            method: 'DELETE',
            url: `http://localhost:8000/update/${id}`,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
            

        }).then(response => {
            navigate('/')
        })
    }

  const submit = () => {

    confirmAlert({
      title: 'Confirm to submit',
      message: 'Are you sure to do this.',
      buttons: [
        {
          label: 'Yes',
          onClick: () => Delete()
        },
        {
          label: 'No',
          //onClick: () => alert('Click No')
        }
      ]
    });
  }

  return (
      <div className='container'>
        <button onClick={submit}>Confirm dialog</button>
      </div>
  );

}

export default App; 
        

【讨论】:

    【解决方案2】:
    let answer = window.confirm("Delete?");
    

    answer 将是 truefalse。所以:

    if (window.confirm("Delete?"))
    { 
       // Here you can put your logic or function that needs to be executed as per the use case.
    }
    

    window.confirm 返回一个布尔值,所以如果它是真的你可以继续,如果它是假的(一旦你点击警报的取消按钮就会出现),警报对话框将关闭。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      相关资源
      最近更新 更多