在删除数据之前提交按钮,reactjs

Submit button before delete data, reactjs

我正在寻找有关如何在删除前向此函数添加提交按钮的答案。我的部分代码:

// func
const deleteData = id => {
    setEditing(false);
    firebase
      .firestore()
      .collection("users")
      .doc(id)
      .delete();
  };

------------------
// and the button without submit option
<button onClick={() => props.deleteData(props.item.id)} />

为了保证实现一个接一个的函数调用,您需要异步编写它们。

这可以通过 async/await 来完成。

试试这个代码片段!

// this will be your submit function
const yourSubmitFunction = () => {
  return new Promise(resolve => {
    // simulate waiting for a submit
    setTimeout(() => {
    
      // resolve after submission
      resolve('submitted!')
    }, 2000)
  })
};

// asynchronous function
const asyncCall = async () => {
  console.log('waiting for submit...')
  const result = await yourSubmitFunction()
  console.log(result)
  
  // .. now you can call your delete function
}

// click and call your asynchronous function
asyncCall()