I'm struggling a bit with using custom react hooks.
I got 2 custom hooks.
First hook is for fetching a ID, the second one is used to fetch a profile with this previous fetched ID. It is dependent on that ID so I need to await this promise.
I have the following custom hook:
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();
}
Then in my second hook I have:
const wallet = UseMetamask();
which is then used in a react-query call like:
useQuery(
['user', wallet],
() => getUserByWallet(wallet),
Now it complains on the wallet about it being a Promise<string | null>
which is ofcourse not suitable for the getUserByWallet
.
What is the go to way to wait for another hook then use that result in a second hook?
Thanks!