Replies: 2 comments 1 reply
-
It could be nice if could make it so that parameters passed to wagmi hook could be either:
It would allow to do this in this kind of situations: const pairResult = useContractRead(
{ addressOrName: ADDRESSES.FACTORY, contractInterface: ABIS.FACTORY },
"getPair",
{ args: pairArgs }
)
const reservesResult = useContractRead(
{ addressOrName: pairResult, contractInterface: ABIS.PAIR },
"getReserves"
) The rules could be as follows:
Note that it would accept any result-like object, which means that it would be compatible with any library returning objects following the React Query structure. @eshlox For a more immediate solution to your problem: do you have the possibility to split it in two components? That way you could only load the component containing the second hook when the first one is loaded: function ComponentA() {
const pairArgs = useMemo(() => [tokenA, tokenB], [tokenA, tokenB]);
const [{ data: pairAddress }] = useContractRead(
{ addressOrName: ADDRESSES.FACTORY, contractInterface: ABIS.FACTORY },
"getPair",
{ args: pairArgs, skip: !tokenA || !tokenB }
);
return pairAddress ? <ComponentB pairAddress={pairAddress} /> : null
}
function ComponentB({ addressOrName }) {
const [{ data: reserves }] = useContractRead(
{ addressOrName, contractInterface: ABIS.PAIR },
"getReserves"
);
// …
} |
Beta Was this translation helpful? Give feedback.
-
@bpierre Thanks for the answer. That's what I'm doing now in some cases. I have a separate component to get the pair address, and when it's ready, it renders the rest of the page, but it's a kind of workaround. 😉 The @tmm maybe it's something to think about when making changes related to #85? 🤔 |
Beta Was this translation helpful? Give feedback.
-
A simple example. Let's say I want to call
getReserves
on pair address.To call
getReserves
, I need the pair's address.The
skip
parameter is useful when arguments are not ready, but what aboutaddressOrName
?The
addressOrName
requires a string. The hook returnsResult | undefined
.I can't pass undefined (
addressOrName
) or an empty string (addressOrName || ""
) because it throws an error:The only option that works is to use the type assertion and pass any correct address if the expected pair address is undefined.
Is there a better way?
Beta Was this translation helpful? Give feedback.
All reactions