'How to prevent React Router DOM from scrolling page to top on navigate replace state?

When I am doing this:

navigate(
  `${window.location.pathname}?query=${encodeURIComponent(
    JSON.stringify(q)
  )}`,
  { replace: true }
);

If I scroll down a little from the page, then change a <Select /> input value, which triggers an update to this query in the URL, react-router-dom is scrolling the page to the top. When I remove this navigate code and change the select inputs, no jumping to the page top occurs, so it must be react-router-dom somehow. How do I disable or prevent this "jump to page top" behavior in react-router-dom?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source