TL;DR: Why can't I use "key_one" in both cases?
When a request fails, it causes weird behavior in the following situation: Parent component and child component use the same query key.
Is there something I've missed? If I use different keys "key_one" and "key_two", my app fails gracefully. If I use the same key, my app falls into a loop. In the working example I've used 'key_one' and 'key_two'. This is not optimal, as in normal circumstances I want them to be the same for caching reasons.
Working code:
async function doFetch() {
throw new Error("Network response was not ok");
}
function MyInnerComp() {
const { isError, error } = useQuery("key_one", doFetch);
return (
<div style={{ border: "1px solid", padding: "10px" }}>
<h2>Hello from inner component!</h2>
{isError && <div>Error! {error.message}</div>}
</div>
);
}
function Comp() {
const { isLoading, isError, error } = useQuery("key_two", doFetch);
if (isLoading) return <div>loading</div>;
return (
<div style={{ border: "1px solid", padding: "10px" }}>
<h2>Parent component.</h2>
{isError && <div>Error! {error.message}</div>}
<hr />
<div>lorem ipsums</div>
<MyInnerComp />
</div>
);
}
Codesandbox: https://codesandbox.io/s/dry-waterfall-21q741?file=/src/index.js
