Search code examples
reactjsfirebasesubmitdelete-row

Submit button before delete data, reactjs


I'm looking for the answer on how to add to this function a submit button before deletion. Part of my code:

// 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)} />

Solution

  • To guarantee achieving one function call after another you'd want to write them asynchronously.

    This can be done with async/await.

    Try out this code snippet!

    // 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()