Replies: 1 comment
-
|
Or... RR8 ditches let fetcher useFetcher({
routeId: 'routes/api.users.$id' // only routes with actions and/or loaders
})
fetcher.fetch({ params: { id: 'abc' }}) // GET
fetcher.fetch({ method: 'post', params: { id: 'abc' } }) // POST
fetcher.loaderData
fetcher.actionData
Although, a GET would need some sort of revalidation opt out flag for it to behave like a GET |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
tl;dr
Background
useFetcherhas no way to be typesafe because thedatachanges based on what you do with it. You can theoretically use the same fetcher instance for both aload()andsubmit()in the same component so unless your route'sactionandloaderhave the same return type, the types will lying to you if you typehint the useFetcher. Example:Here,
fetcher.datawould be the loader's return data but it's typed as if the action will be called.Abstraction
For this to work, the intent of the request needs to be established at fetcher initialization.
However, if this were done to
useFetcher, the methods make less sense:I believe this warrants a new utility.
Proposal
A new,
useRouteHandlerhook that would be configured just enough before use to be typesafe.We should be able to use fetchers under the hood. Something like this. (useMemo's removed for clarity)
This version still has distinct methods for calling an action(
submit) vs a loader(load) but they're mutually exclusive.Considerations
I know some people prefer the distinction at the callsite but it could probably be normalized into a common function who's arg type changes based on how it's configured.
The
fetchfunction could also be namedsubmitsince that makes sense for all verbs considering you cansubmitagetrequest with a fetcher now.searchParams
Kinda goofy since they can be part of the action and also become the payload for GET submissions. I'm not sure the best way to handle them.
It might make sense to separate them out at the callsite(if we want to support them there) and adjust the
bodytype.Having a dedicated place to put searchParams for both requests might make more sense from a consistency standpoint. Also matches better with builtin fetch() since bodies are ignored with GET requests.
Something like this.
Alternative
One alternative would be to try to enhance useFetcher and add
loaderDataandactionDatato its return.However, with this approach i think we'd want typesafey on existing methods
Beta Was this translation helpful? Give feedback.
All reactions