I'm new to RTK query and I'm having issues with it sharing data across queries with different args.
// api.js
const { useGetStuffQuery } = api.injectEndpoints({
endpoints: (builder) => ({
getStuff: builder.query({
query: (stuffId) => ({ url: `http://some.random-url.com/${stuffId}` }),
}),
}),
});
// component.js
function(props) {
const [id, setId] = useState(1);
const { data, isLoading } = useGetStuffQuery(id);
if (val === '2') {
console.log('datttt', data); // this initially prints data from id:1
}
return <div>
<input value={id} onChange={(e) => setId(e.target.value)} />
{
isLoading ? <span>loading...</span>
: <span>data is: {data}</span>
}
</div>
}
I was expecting to see the loading indicator to show up once I changed the id from 1 to 2.
However, it seems like rtkQuery is still using the cached data from my previous useGetStuffQuery(1)
call and hence I see the data for id1 momentarily. Once the request for id2 is resolved -- it does update and rerender my component.
Is this caching behavior intentional and expected? Or am I missing something?