简体   繁体   中英

How to set the transaction hash with useState in an Ethereum transaction

I would like to set a variable with React hooks. Whenever I send an Ethereum transaction it is possible to get the hash of it through the method on('transactionHash', {...}) but if I try to call setTransactionHash nothing happens. Here is the code:

import React, {useState, useEffect, useCallback} from 'react';
import collegamentoConDb from '../api/collegamentocondb';
const Admin = ()=>{

    const [transactionHash, setTransactionHash] = useState("");
    ...
    try{
      window.ethereum.enable();
      const accounts = await web3.eth.getAccounts();
      await contract.methods.notarizzazione(hashCampi).send({
        from:accounts[0]
      }).on('transactionHash', (hash)=>{
        setTransactionHash(hash)
      });
    
} catch(err){
  console.log(err);
}
//db call

   const fileTestoPdf = await collegamentoConDb.post('/creapdf', {id, transactionHash}); 
   ...

When I read the log file, transactionHash is not updated and I do not understand why. Can someone help me?

Thank you.

Could you get hash in the console? if yes then try const [transactionHash, setTransactionHash] = useState();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM