In React Typescript (.tsx file), I'm trying to use web3.eth.sign
to validate an account. The signature is being generated properly, but I can't seem to get the signature out of the callback function.
The following code is inside a functional component:
const [signature, setSignature] = useState<string>('')
function Sign() {
const web3 = window.web3
web3.personal.sign(
nonce,
accountId,
function(err: any, signed: string) {
if (!err) {
console.log(`${signed}`)
setSignature(signed)
console.log(`Signature = ${signature}`)
}
})
}
Sign()
// some other code that uses signature variable
signed
is printing a valid signature, but signature
is not being set. This is a big problem. For the rest of my code to work I need to get the signed
variable and set it to an external signature
variable where I can pass it to other functions.
Any ideas?