我在使用自定义反应挂钩方面遇到了一些困难。
我有 2 个定制挂钩。
第一个钩子用于获取 ID,第二个钩子用于使用先前获取的 ID 来获取配置文件。它依赖于该 ID,所以我需要等待这个承诺。
我有以下自定义挂钩:
export const UseMetamask = () => {
//Todo: Create check if metamask is in browser, otherwise throw error
const fetchWallet = async (): Promise<string | null> => {
try {
const accounts: string[] = await window.ethereum.request(
{
method: 'eth_requestAccounts'
},
);
return accounts[0];
} catch(e) {
console.error(e);
return null;
}
}
return fetchWallet();
}
Run Code Online (Sandbox Code Playgroud)
然后在我的第二个钩子中我有:
const wallet = UseMetamask();
Run Code Online (Sandbox Code Playgroud)
然后在反应查询调用中使用它,例如:
useQuery(
['user', wallet],
() => getUserByWallet(wallet),
Run Code Online (Sandbox Code Playgroud)
现在它抱怨钱包说它是一个Promise<string | null> …