-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Description
What version of Remix are you using?
1.9.0
Background Context
While creating my own custom hook to detect when useSubmit is done responding from the server, I found that useTransition().type doesnt expose a done value, unlike fetcher.type
// Easier to implement since `done` is exposed on `fetcher.type`
const {submit} = useRemixFetcher({
onSuccess(){..},
onError(){..}
})
// harder to implement since `done` is not exposed on `useTransition.type`
const {submit} = useRemixSubmit({
onSuccess(){..},
onError(){..}
})Expected Behavior
After calling useSubmit and successfully receiving a response back from the server, useTransition's useTransition().state value should be idle and the useTransition().type value should be done, eventually
Actual Behavior
After calling useSubmit() and successfully receiving a response back from the server, useTransition.type value does not get updated to done to indicate inform our remix app that the form submission has successfully completed, instead useTransition.type === 'idle'.
Steps to Reproduce
Stackblitz URL: https://stackblitz.com/edit/node-s1xxwy?file=package.json,app%2Froutes%2Findex.tsx
Testing useSubmit
- Click
getDatabutton inside theChild1Componentcontainer - View
console.log's, you'll see thattranstion.typewill never have adonevalue
Testing useFetcher
- Click
getDatabutton inside theChild2Componentcontainer - View
console.log's, you'll see thattranstion.typewill never have adonevalue
Video Reproduction
useSubmit--vs--useFetcher---type-value.mp4
Why is useTransition().type === 'done' important?
1.Currently fetcher.type exposes done after a full form submission has completed:
https://github.com/remix-run/remix/blob/main/packages/remix-react/transition.ts#L121-L247
2.Having a done state is useful in user land, as it allows use to tap into when a submission completed from the backend.
For example, you can use fetcher.type === 'done' to trigger an onSuccess or onError callback in your code and show a Toast to the user after the server has responded successfully or with an error.
I created a custom lightweight hook around useFetcher called useRemixFetcher which allows a user specify onSuccess and onError callbacks like this & I use fetcher.type === 'done' to know when my submission has completed in order to fire my callbacks.
// DEMO: https://stackblitz.com/edit/node-s1xxwy?file=package.json,app%2Froutes%2Findex.tsx
const { submit, state } = useRemixFetcher({
onSuccess: () => {
toast.success('π Success')
},
onError: (error) => {
toast.error('β Error')
},
});Currently it's more complicated to detect when a useSubmit has completed due to done not being exposed to transition.type Stackblitz URL: https://stackblitz.com/edit/node-s1xxwy?file=package.json,app%2Froutes%2Findex.tsx
Follow up questions
I'm aware that useTransition is a global singleton whereas useFetcher's are completely independent of other useFetcher's
1.I was curious if this omission of done on useTransition is intentional?
2.I think we should expose it to TransitionStates just like how its exposed in useFetcher's FetcherStates type
