New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: React Router 6 useLoaderData returning cached data after fresh data is returned by the loader function #11518
Comments
I am also experiencing this issue |
Loader data ia only revalidated after mutations (POST's) - but in these examples you're not performing a POST submission. In the code above, you don't have a So, the fetcher is submitting a GET submission to the index route and executing the In the codesandbox you did correctly have The fix is to use I this is just a searchbox type UI and there's nothing to mutate, then your best bet is to use the route loader data initially and prefer the updated fetcher data once available. let data = useLoaderData();
let fetcher = useFetcher();
let mostRecentData = fetcher.data || data; |
Thanks @brophdawg11 this is new territory for me as it seems like we are trying to simulate the default behaviors for html forms, so by that logic this makes sense. |
Another simpler option I forgot to mention is that for a searchbox UI you can also just use navigations and skip the fetcher. |
@brophdawg11 I think that is what I was looking for, but somehow I started looking at useFetcher as it could serve CRUD altogether. I would suggest that RR6 docs need to be a bit more fleshed out for an easier description. |
What version of React Router are you using?
6.22.3
Steps to Reproduce
Edit:
Just to clarify the loader function itself runs correctly in the index route and returns the correct userData. However the data returned inside the component via useLoaderData is incorrect after the fetcher.submit calls.
As requested below is a link to a codeSandbox link that demonstrates the issue:
CodeSandBox Link
Below is the create router:
then the index route:
Below is the RangePicker component:
Expected Behavior
New data should be passed to child components by useLoaderData
Actual Behavior
Stale data fetched first time is returned by useLoaderData.
The text was updated successfully, but these errors were encountered: