Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Bug]: useSearchParams reverts to default value when removing the last value #9668

Closed
eddiemonge opened this issue Dec 2, 2022 · 5 comments · Fixed by #9969
Closed

[Bug]: useSearchParams reverts to default value when removing the last value #9668

eddiemonge opened this issue Dec 2, 2022 · 5 comments · Fixed by #9969
Labels

Comments

@eddiemonge
Copy link

eddiemonge commented Dec 2, 2022

What version of React Router are you using?

6.4.4

Steps to Reproduce

Link to example (edit mode)

  1. Call useSearchParams with a default initialization like const [searchParams, setSearchParams] = useSearchParams({ option: 'One' });
  2. Change the value of searchParams.get('option'). The value should change to the new value
  3. Remove the param key. The value should be remove. In actuality, the default initialization is restored
import * as React from 'react';
import { useSearchParams } from 'react-router-dom';

export default function App() {
  const [searchParams, setSearchParams] = useSearchParams({ option: 'One' });
  console.log('initial search params', searchParams.toString())

  const handleChange = ({ target: { value } }: React.ChangeEvent<HTMLInputElement>) => {
    if (value !== 'All') {
      console.log('Setting the option to', value)
      setSearchParams({ option: value});
    } else {
      console.log('Clearing the option')
      setSearchParams({});
    }
  };

  return (
    <div>
      <h1>Search Param Value</h1>
      <p>{searchParams.toString()}</p>
      <h1>Choose an option</h1>
      {['One', 'Two', 'All'].map((s) => (
        <div key={s}>
          <label>
            <input
              checked={searchParams.get('option') === s}
              name="choice"
              onChange={handleChange}
              type="radio"
              value={s}
            />
            <span>{s}</span>
          </label>
        </div>
      ))}
    </div>
  );
}

Expected Behavior

The search params would be blank and searchParam would be an empty object-ish.

Actual Behavior

searchParam reverts to option: 'One'

edited to include a working example of the bug and instructions Jan 23, 2023

@eddiemonge eddiemonge added the bug label Dec 2, 2022
@remix-run remix-run deleted a comment from yasu2122yasu Dec 8, 2022
@remix-run remix-run deleted a comment from yasu2122yasu Dec 8, 2022
@brophdawg11
Copy link
Contributor

Could you provide a working reproduction of this in code sandbox, ideally without any component library dependencies?

@eddiemonge
Copy link
Author

@brophdawg11 added

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.7.1-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!

@brophdawg11 brophdawg11 added the awaiting release This issue have been fixed and will be released soon label Jan 24, 2023
@eddiemonge
Copy link
Author

thanks for the quick fix

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.8.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!

@brophdawg11 brophdawg11 removed the awaiting release This issue have been fixed and will be released soon label Jan 26, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants