Disable fetch on mount but fetch on variable change without using refetch #805
-
I have 2 requests to make. The first, imageQuery, is sent on mount and returns a url. The second, imageProxyQuery, is not sent on mount but instead after the first is successful and requiring the url as payload. Request calls are in hooks not shown. It seems that when a query is Is it possible to disable on mount but fetch on variable change?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @eoghanmccarthy const [imageUrl, setImageUrl] = useState();
const imageQuery = useImage(imageId, {
onSuccess: res => {
setImageUrl(res.data.imageUrl);
}
});
const imageProxyQuery = useImageProxy(imageUrl, {
- enabled: false,
+ enabled: imageUrl,
}); See https://react-query.tanstack.com/docs/guides/queries#dependent-queries Also, you might not need that imageUrl state - it can be derived from imageQuery: const imageQuery = useImage(imageId);
const imageUrl = imageQuery.data ? imageQuery.data.imageUrl : null;
const imageProxyQuery = useImageProxy(imageUrl, {
enabled: imageUrl,
}); |
Beta Was this translation helpful? Give feedback.
Hi @eoghanmccarthy
Did you try something like this?
See https://react-query.tanstack.com/docs/guides/queries#dependent-queries
Also, you might not need that imageUrl state - it can be derived from imageQuery: