英文:
Wagmi useContractEvent listener can't access useState variable
问题
I'm stuck on this problem that I really can't make sense of.
当尝试从useContractEvent中的listener函数中访问hash状态变量时,它是未定义的。
我可以在useContractEvent之外的地方访问hash状态变量,但是一旦事件被触发并且我尝试在listener函数内部读取hash状态变量,它就变成了未定义的。
这是上下文问题吗?
英文:
I'm stuck on this problem that I really can't make sense of.
When trying to access the hash state variable from the listener function in useContractEvent it is undefined.
I can access the hash state variable outside of the listener function in useContractEvent but as soon as the event is triggered and I try to read the hash state variable inside the listener function it is undefined.
Is it a context issue?
const [hash, setHash] = useState<`0x${string}` | undefined>();
useContractEvent({
address: contract.address as `0x${string}`,
abi: contract.abi,
eventName: "SomeEvent",
listener(log) {
console.log(hash) // undefined
if (log[0]?.transactionHash != hash) return;
// never reached
},
});
const { write } = useContractWrite({
address: contract.address as `0x${string}`,
abi: contract.abi,
functionName: "someFunction",
args: [],
onSuccess(data) {
setHash(data.hash);
},
});
return (
<button onClick={() => write?.()}>
Go
</button>
);
答案1
得分: 1
Yes, the issue you're facing is likely due to a context problem related to closures and the asynchronous nature of useContractEvent and useContractWrite.
一种方法是使用 ref
来始终访问 hash
的最新值。
const hashRef = useRef<`0x${string}` | undefined>();
useContractEvent({
address: contract.address as `0x${string}`,
abi: contract.abi,
eventName: "SomeEvent",
listener: (log) => {
console.log(hashRef.current); // 这将始终记录 `hash` 的最新值
if (log[0]?.transactionHash !== hashRef.current) return;
// 不会执行到这里
},
});
const { write } = useContractWrite({
address: contract.address as `0x${string}`,
abi: contract.abi,
functionName: "someFunction",
args: [],
onSuccess(data) {
hashRef.current = data.hash;
},
});
return <button onClick={() => write?.()}>Go</button>;
英文:
Yes, the issue you're facing is likely due to a context problem related to closures and the asynchronous nature of useContractEvent and useContractWrite.
One approach is to use ref
to always access the latest value of hash
const hashRef = useRef<`0x${string}` | undefined>();
useContractEvent({
address: contract.address as `0x${string}`,
abi: contract.abi,
eventName: "SomeEvent",
listener: (log) => {
console.log(hashRef.current); // This will always log the latest value of `hash`
if (log[0]?.transactionHash !== hashRef.current) return;
// never reached
},
});
const { write } = useContractWrite({
address: contract.address as `0x${string}`,
abi: contract.abi,
functionName: "someFunction",
args: [],
onSuccess(data) {
hashRef.current = data.hash;
},
});
return <button onClick={() => write?.()}>Go</button>;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论