You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Revalidation triggers fetcher.submit to changeΒ #4872
Observe the console shows that fetcher.submit has changed.
Expected Behavior
I expect fetcher.submit to be stable so I can safely use it in dependency arrays.
Actual Behavior
I cannot use fetcher.submit in a dependency array because any time there's a revalidation on the page, all fetcher.submits are new instances of the submit function.
So, what's happening, is I make a change to the URLSearchParams, that triggers the defaultAction to change which invalidates the submit on the fetcher which triggers my useEffect again.
In my case, I don't care about the defaultAction because I specify one when I call submit.
So, in order to ensure referential stability, we need to either:
allow the user to configure the action when calling useFetcher so we can ignore the defaultAction in that case (so it won't appear in the dependency array and therefore cannot change)
Or we need to use a "latest ref" pattern so we don't have to list the defaultAction in the dependency array.
I much prefer option 1 because it would actually be a really nice feature to have anyway. It would allow folks to change their code from something like this:
I've seen some folks suggest they would actually prefer this API and I think I agree with them. Also, it would solve the problem of fetcher being referentially stable.
Reacted by Shivam Jha, Jody Geraldo, Christian Stoyanov, J.D Nicholls and Emily Armstrong
Activity
kentcdodds commentedon Dec 15, 2022
After further investigation, what's trigger
fetcher.submitto change is thedefaultActiondependency:remix/packages/remix-react/components.tsx
Line 1260 in 8aacba3
So, what's happening, is I make a change to the URLSearchParams, that triggers the
defaultActionto change which invalidates thesubmiton the fetcher which triggers myuseEffectagain.In my case, I don't care about the
defaultActionbecause I specify one when I callsubmit.So, in order to ensure referential stability, we need to either:
actionwhen callinguseFetcherso we can ignore thedefaultActionin that case (so it won't appear in the dependency array and therefore cannot change)defaultActionin the dependency array.I much prefer option 1 because it would actually be a really nice feature to have anyway. It would allow folks to change their code from something like this:
To this:
I've seen some folks suggest they would actually prefer this API and I think I agree with them. Also, it would solve the problem of fetcher being referentially stable.
brophdawg11 commentedon Apr 14, 2023
This should be resolved by remix-run/react-router#10336 which makes
fetcher.submitstable acrosslocationchangesbrophdawg11 commentedon Apr 21, 2023
Should be available in Remix 1.16 once released