const {address} = useAccount({
onConnect() {
setUserAddress(address)
}
})
const {data: req, refetch} = useContractReads({
watch: true,
contracts: [
{
...myContract,
functionName: 'myValue1',
},
{
...myContract,
functionName: 'myValue2',
},
{
...anotherContract,
functionName: "allowance",
args: [userAddress, myContractAddress],
}
],
onSuccess(req){
setMyValue1(req[0].result),
setMyValue2(req[1].result),
setAllowanceMyContract(req[2].result)
}
});
每当连接的钱包发生变化时,我都需要重新触发上述
useContractReads()
,以便重新获取津贴,需要执行合约功能而不触发区块链级别的错误。如果页面缓存了之前的配额,则 dApp 认为它有配额。但是当我从前端调用合约函数时,我从区块链中收到一个津贴错误,并且 Metamask 不会弹出以继续批准。
我找到了一个很好的片段来检测帐户更改,但仍然没有重新获取数据的解决方案。 检测帐户更改片段:
const { connector: activeConnector } = useAccount();
useEffect(() => {
const handleConnectorUpdate = ({ account, chain }: ConnectorData) => {
if (account) {
console.log("new account", account);
} else if (chain) {
console.log("new chain", chain);
}
};
if (activeConnector) {
activeConnector.on("change", handleConnectorUpdate);
}
return () => activeConnector?.off("change", handleConnectorUpdate) as any;
}, [activeConnector]);
关于重新获取,该站点的用户建议使用此解决方案,但在我的情况下,它会引发异常错误。 如何在 wagmi 中重新查询查询
错误:
Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.invokeGetter
useEffect 怎么样?您可以追踪地址。
const { address, isConnected } = useAccount();
useEffect(() => {
//do something
}, [address])