Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: history.push no longer triggers a re-render when a search param has changed #7918

Closed
mattp94 opened this issue Jul 23, 2021 · 3 comments

Comments

@mattp94
Copy link

mattp94 commented Jul 23, 2021

What version of React Router are you using?

5.2.0

Steps to Reproduce

Hello 馃憢, I observed a strange behavior after upgrading react-router-dom from v5.1.2 to v5.2.0. It looks like history.push or history.replace (taken from useHistory) doesn't trigger anymore a re-render when a search param has changed.

Expected Behavior

Here is a simple example with v5.1.2:
https://codesandbox.io/s/with-react-router-dom-512-before-zljp8

As you can see, calling history.push changes the URL as well as the counter value. So everything works well!

Actual Behavior

Now, when I test with v5.2.0, it no longer works:
https://codesandbox.io/s/with-react-router-dom-520-after-z796t

Here, calling history.push changes the URL but doesn't re-render the counter... It's a bit annoying as this minor version has introduced a breaking change in our application.

Is it normal? 馃檪

@mattp94 mattp94 changed the title [Bug]: history.push no longer triggers a render when a search param has changed [Bug]: history.push no longer triggers a re-render when a search param has changed Jul 23, 2021
@renatobenks
Copy link

renatobenks commented Jul 24, 2021

It might be related to #7103 or #7288, but much probably was the update of mini-create-react-context because the changes over useLocation were minimal and basically entire related to just create a context from mini-create-react-context as long as the problems seem to be on the context re-render updating once the history.push is correctly working.

What do you guys think @timdorr and @mjackson? There's anything wrong with the code in those codesandbox links?

@timdorr
Copy link
Member

timdorr commented Jul 24, 2021

You're not subscribing to any location change. You're relying on an old performance-related bug that we fixed in 5.2. Use the useLocation hook instead and it works just fine:

export default function App() {
  const history = useHistory();
  const location = useLocation();

  const searchParams = new URLSearchParams(location.search);
  const counter = Number(searchParams.get("counter"));

  const handleClick = () => {
    searchParams.set("counter", counter + 1);
    history.push("?" + searchParams);
  };

  return (
    <>
      <button onClick={handleClick}>Increment</button>
      {counter}
    </>
  );
}

@timdorr timdorr closed this as completed Jul 24, 2021
@mattp94
Copy link
Author

mattp94 commented Jul 26, 2021

Thanks a lot @timdorr 馃憤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants