Skip to content

Conversation

promet99
Copy link
Contributor

@promet99 promet99 commented May 28, 2022

What this PR does

This PR modifies setSearchParams to support functional updates, just like setState of React.useState.
setSearchParams can take a function of type ((prev: URLSearchParams) => URLSearchParamsInit) as an input.
When given a function of aforementioned type, setSearchParams will provide current searchParams to the given function, and its returned value will be set as searchParams.

Why this PR is made

  1. Current react-router documentation makes an analogy between useState and useSearchParams. Adding this to minor feature will further accommodate the analogy.
  2. I personally have seen&used this feature being implemented in several different codebases. It proved to be useful.
  3. Only one small logic and several lines of code are added, so this change will keep react-router slim.

What can be further done

This change can also be implemented to react-router-dom package too.
Not having used react-router-dom package before, I have excluded it from this PR.

Etc

Feature, test, and documentation are all included.

@promet99 promet99 changed the title Feat: add functional update feature to useSearchParams,like React.useState Feat: useSearchParams supports functional updates like React.useState May 28, 2022
@brophdawg11
Copy link
Contributor

Hey @promet99! It looks like this has some conflicts with the 6.4.0 work that was just merged down into dev - would you mind taking a look at those?

@ryanflorence
Copy link
Member

Looks good, @brophdawg11 let's get this over the finish line.

@brophdawg11 brophdawg11 self-assigned this Jun 8, 2022
@brophdawg11 brophdawg11 changed the base branch from dev to brophdawg11-functional-searchparams June 8, 2022 15:02
@brophdawg11
Copy link
Contributor

Thank you for the PR @promet99! I'm going to merge this to an interim branch so I can fix up the conflicts and handle the react-router-native side of things, and then I'll PR the interim branch to dev with both sets of changes 👍

@brophdawg11 brophdawg11 merged commit 2eb7e37 into remix-run:brophdawg11-functional-searchparams Jun 8, 2022
@promet99
Copy link
Contributor Author

promet99 commented Jun 8, 2022

@

Thank you for the PR @promet99! I'm going to merge this to an interim branch so I can fix up the conflicts and handle the react-router-native side of things, and then I'll PR the interim branch to dev with both sets of changes 👍

Thanks a lot!

@promet99 promet99 deleted the dev branch June 8, 2022 15:13
brophdawg11 added a commit that referenced this pull request Jun 8, 2022
* Feat: useSearchParams supports functional updates like React.useState (#8909)

* [Feat] add functional update to useSearchParams

* add contributor

* add update for react native

* docs

Co-authored-by: Chris Park <54811538+promet99@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants