I need call contract function in useEffect
How to get same result with useDapp ? Any example of this case ?
// define standard web3 call
const feth = async (address) => {
const contract = new.web3.eth.Contract(ContractAddress, ContractABI)
const response = await contract.methods.getDataByUserAddress(address).call()
return response
}
// call when page open
useEffect(() => {
if(isAddress(props.match.params.address))
fetch(props.match.params.address)
}, [props.match.params.address]);
If I understand correctly, it is about this library - useDApp.
In your example you use
web3.js, but as I understood from the documentation foruseDApp, it works withethers.js.To use the library you need to install it and ethers:
Further it is quite simple, here is a sample from the documentation with some changes for your question:
The basic idea here is that we use the
useContractFunctionhook which allows us to send a transaction that executes a function of a contract on a blockchain.To use
useContractFunctionyou must provide a contract object from theether.jslibrary and a string function name.It returns an object, which you can destruct and get from there
sendmethod andstateobject:sendis used to make requests and matches all arguments to solidity contract arguments.stateobject can be used to track the status of a transaction.You can read more about all the hooks and methods in the documentation of the library