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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Regression - useNavigate is not stable #10767

Closed
codebutler opened this issue Aug 7, 2023 · 1 comment
Closed

[Bug]: Regression - useNavigate is not stable #10767

codebutler opened this issue Aug 7, 2023 · 1 comment
Labels

Comments

@codebutler
Copy link

What version of React Router are you using?

6.14.2

Steps to Reproduce

Hi, if I am understanding the conversation in #10743 correctly, useNavigate should be stable while navigating when using RouterProvider, but that's not the behavior I'm seeing.

Simple example: https://replit.com/@codebutler/React-Router-useNavigate-re-render-bug#src/App.tsx

Expected Behavior

Root component should not re-render when location changes.

Actual Behavior

Root component re-renders.

@codebutler codebutler added the bug label Aug 7, 2023
@brophdawg11
Copy link
Contributor

You're confusing "stability" with "re-rendering". React always re-renders when state changes. The state changing is the global router state and it performs a top-down re-render. This is also what happens on your context access. This comment in the original issue speaks a bit to this: #7634 (comment).

The navigate function is stable when using RouterProvider which means that it's identity will not change across navigations and thus it will not trigger re-executions of effects on navigations when it's used as a dependency.

let navigate = useNavigate();
console.log("render"); // This will log on location changes

useEffect(() => {
  console.log("effect"); // This will not log on location changes
}, [navigate]);

@brophdawg11 brophdawg11 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants