“Wagmi useContractEvent监听器无法访问useState变量”

huangapple go评论100阅读模式
英文:

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?

  1. const [hash, setHash] = useState<`0x${string}` | undefined>();
  2. useContractEvent({
  3. address: contract.address as `0x${string}`,
  4. abi: contract.abi,
  5. eventName: "SomeEvent",
  6. listener(log) {
  7. console.log(hash) // undefined
  8. if (log[0]?.transactionHash != hash) return;
  9. // never reached
  10. },
  11. });
  12. const { write } = useContractWrite({
  13. address: contract.address as `0x${string}`,
  14. abi: contract.abi,
  15. functionName: "someFunction",
  16. args: [],
  17. onSuccess(data) {
  18. setHash(data.hash);
  19. },
  20. });
  21. return (
  22. <button onClick={() => write?.()}>
  23. Go
  24. </button>
  25. );

答案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 的最新值。

  1. const hashRef = useRef<`0x${string}` | undefined>();
  2. useContractEvent({
  3. address: contract.address as `0x${string}`,
  4. abi: contract.abi,
  5. eventName: "SomeEvent",
  6. listener: (log) => {
  7. console.log(hashRef.current); // 这将始终记录 `hash` 的最新值
  8. if (log[0]?.transactionHash !== hashRef.current) return;
  9. // 不会执行到这里
  10. },
  11. });
  12. const { write } = useContractWrite({
  13. address: contract.address as `0x${string}`,
  14. abi: contract.abi,
  15. functionName: "someFunction",
  16. args: [],
  17. onSuccess(data) {
  18. hashRef.current = data.hash;
  19. },
  20. });
  21. 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

  1. const hashRef = useRef&lt;`0x${string}` | undefined&gt;();
  2. useContractEvent({
  3. address: contract.address as `0x${string}`,
  4. abi: contract.abi,
  5. eventName: &quot;SomeEvent&quot;,
  6. listener: (log) =&gt; {
  7. console.log(hashRef.current); // This will always log the latest value of `hash`
  8. if (log[0]?.transactionHash !== hashRef.current) return;
  9. // never reached
  10. },
  11. });
  12. const { write } = useContractWrite({
  13. address: contract.address as `0x${string}`,
  14. abi: contract.abi,
  15. functionName: &quot;someFunction&quot;,
  16. args: [],
  17. onSuccess(data) {
  18. hashRef.current = data.hash;
  19. },
  20. });
  21. return &lt;button onClick={() =&gt; write?.()}&gt;Go&lt;/button&gt;;

huangapple
  • 本文由 发表于 2023年8月4日 20:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定