fetcher revalidation after actions #2775
Replies: 7 comments 11 replies
-
FYI: we've implemented this in React Router's data APIs and it'll come to Remix when we bring over the new Router to Remix. |
Beta Was this translation helpful? Give feedback.
-
Remix 1.10.0 now includes automatic revalidation of fetchers, but it's turned on for all fetchers and there doesn't seem to be a way to override it (Ryan's proposal above included a In the same way we can use |
Beta Was this translation helpful? Give feedback.
-
I had to use axious to send data to the api without refetching my current page :(. That is sad that api doesn't allow to update data which doesn't require to revalidate at least current page. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if there is an issue related to this in my current project where we have a fetcher.Form to send an email (ajax is just fine for this one and we don't need to refresh the page) and after the post to the action there are some attempts to call all the loaders for this nested route but all of them are being cancelled (they reach the server anyway): |
Beta Was this translation helpful? Give feedback.
-
Even if I'm not sure how infinite loaders with actions should be implemented. Currently I'm submitting actions using vanilla fetch() not to trigger any revalidation and handle data refreshes and caching myself, but that becomes very complex very quickly. |
Beta Was this translation helpful? Give feedback.
-
Strange to me that this is the default behaviour, at least how I intend to use fetchers. Solved by putting shouldRevalidate = false on the fetcher route, but this wouldn't be possible if using the UI route as also the fetcher route, because as @dantrain said, there seems to be no way to distinguish between the default loader and fetcher loader when using the shouldRevalidate args. Agree with others that useFetcher should have a revalidate option. I would say default to off, but either way need the ability to specify this. |
Beta Was this translation helpful? Give feedback.
-
Yep just use a normal const formData = new FormData()
formData.append(...)
fetch('/your/endpoint', { method: 'POST', body: formData }) |
Beta Was this translation helpful? Give feedback.
-
Right now, if an action is called (
<Form>
,submit
,fetcher.submit
,<fetcher.Form>
) all of the loader data for the currently rendered routes are revalidated.However ... any
fetchers
that have calledfetcher.load()
do not revalidate and it takes a bit of React Fu to get it to.I've had a hunch since the beginning the fetchers with loaded data should be able to revalidate with actions as well. I hadn't taken the time to really think through use cases though. But then Santi sent me a perfect use case:
I would design this page something like this:
This is great, until you click the star button. In either place because only the child route data will revalidate.
To make this work today, you'd need to pass a callback in to the child component to trigger the reload of the fetcher:
Proposal
I think
useFetcher
should be able to participate in revalidation just like route loaders already do.It could look like this:
shouldReload
continues to determine if it should revalidate or not, this way it doesn't matter which loader is called. After all, the idea ofshouldReload
is really only known by the loader to know what it depends on.The only change this app requires now is simply telling the fetcher to participate in revalidation:
Whichever star you click will call an action, routes will revalidate and the infinite scrolling list
useFetcher
will be revalidated as well.Implementation Notes
There will need to be new internal state inside of transition manager to remember the last arg passed to
fetcher.load()
. After an action, iterate through the current fetchers and callload
on the ones that want to participate in revalidation.I think we should probably do this over in React Router as we're about to ship all of these data APIs over there and then it'll come back to Remix when we implement the new router.
Beta Was this translation helpful? Give feedback.
All reactions