Skip to content

[Bug]: keep fetcher stable on location changesΒ #9739

Closed

Description

@kentcdodds

What version of React Router are you using?

https://stackblitz.com/edit/github-gr13fk?file=src%2Fmain.tsx

Steps to Reproduce

Click the button and see the error

Expected Behavior

The useEffect should not run after changing the search params

Actual Behavior

The fetcher changes when the location changes due to the submitter changing.

Basically, due to the defaultAction changing and that being included in the dependency array, it causes the submit callback to be changed.

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. This impacts any situation you want to use fetcher in a dependency array as well.

After further investigation, what's trigger fetcher.submit to change is the defaultAction dependency: https://github.com/remix-run/remix/blob/8aacba3c4cd23e63ef57a8cb7b68c9d6c88bdb6e/packages/remix-react/components.tsx#L1260 (at least that's what it is in Remix, probably similar in react router).

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.

Originally discussed in #9737

Activity

  1. self-assigned this
    on Dec 16, 2022
  2. brophdawg11 commented on Apr 10, 2023

    @brophdawg11
    Contributor

    Related comment for stabilizing useNavigate: #7634 (comment)

  3. brophdawg11 commented on Apr 14, 2023

    @brophdawg11
    Contributor

    Fixed by #10336

  4. added
    awaiting release
    on Apr 14, 2023
  5. brophdawg11 commented on Apr 17, 2023

    @brophdawg11
    Contributor

    Reopening until this is released to npm

  6. brophdawg11 commented on Apr 21, 2023

    @brophdawg11
    Contributor

    Aligning with Remix here - will close this issue with awaiting release. This will be available when React Router 6.11 is released.

  7. added
    awaiting release
    and removed
    awaiting release
    on Apr 21, 2023
  8. removed their assignment
    on Apr 21, 2023
  9. github-actions commented on Apr 26, 2023

    @github-actions
    Contributor

    πŸ€– Hello there,

    We just published version 6.11.0-pre.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

    Thanks!

  10. github-actions commented on Apr 28, 2023

    @github-actions
    Contributor

    πŸ€– Hello there,

    We just published version 6.11.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

    Thanks!

  11. removed
    awaiting release
    on Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions