-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(remix-react): add submitOptions
argument for useSubmit
and useFetcher
#4882
Conversation
🦋 Changeset detectedLatest commit: 354482b The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Remember to target the |
export function useSubmit(): SubmitFunction { | ||
return useSubmitImpl(); | ||
export function useSubmit(options?: SubmitOptions): SubmitFunction { | ||
let key = undefined; // no key for global submit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about making key
a part of the options object in useSubmitImpl
instead of accepting it as a first param, but decided to not cause a breaking change to useSubmitImpl
because it's exported and it's possible people are using it. I actually don't hate this though so maybe it's fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I think we can safely remove the export (as it was never intended to be a public API), I think we can make key
part of the submitOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually kinda like that submitOptions
is a separate argument, even though we have this funny key = undefined
thing. But I don't care enough.
const DEFAULT_METHOD = "get"; | ||
const DEFAULT_ENC_TYPE = "application/x-www-form-urlencoded"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched these to const
and upper-case because otherwise typescript wasn't happy about default-assigning submitMethod
to DEFAULT_METHOD
below. Using const
here allows typescript to narrow this down from a type of string
to "get"
which fits in the FormMethod
type.
let defaultFormAction = useFormAction(); | ||
let defaultAction = submitAction || defaultFormAction; | ||
let defaultMethod = submitMethod || DEFAULT_METHOD; | ||
let defaultEncType = submitEncType || DEFAULT_ENC_TYPE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By putting these calculations all here we can avoid having to pass defaultFormAction
into the dependency array which is what fixes my original issue.
1ed43e3
to
4cd38da
Compare
4cd38da
to
70d32b1
Compare
Sorry, totally forgot to target |
submitOptions
argument for useSubmit
and useFetcher
Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
We're about to wipe this code out when we merge the 6.4 work 😬 - so if we're going to adopt this we probably need to do it in |
Yep. I expected as much. Didn't really think this would get merged as-is. Here's the discussion over there: remix-run/react-router#9737 |
Closing this as it will be implemented in RR |
This fixes an actual bug I was having when trying to use a fetcher in a
useEffect
. It's also a handy API that I've seen people ask for as well.Closes: #4872
Testing Strategy: Automated Integration test
This is just pushing the failing test to demonstrate the issue.The issue is actually technically correct behavior, but I need a new feature to accomplish what I want so I'll be changing the test once this new API is supported. I'm working on that next.Implementation is finished and documented. Ready to discuss/merge.
Until this is merged, the only way to reliably use a fetcher.submit in a dep array is to do something like this:
The drawback here is that if we actually do depend on the
defaultAction
then it's possible we'll get the wrong one when submit is called.This PR fixes that by allowing users to specify an
action
(among other submit options):