-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
SPA Mode + <NavLink to="/" end>
rendered in root.tsx
Layout component will always start as "active"
#13010
Comments
As you commented, this is essentially a "misuse" of NavLink within the root module. And you are also correct that we have poor documentation here. We have #13000 open to track adding that into our docs. I'm not sure what a good fix is here, as the root module is being rendered down to plain HTML and will be static upon first load. We choose the root URL to render against, so that is why it is active despite the different URL in the browser. Maybe a warning of some sort could be added? I'm not sure how possible that might be. |
As mentioned this would be expected on first load, but I'm unsure why it's not flipping over on hydration. Duplicating the layout export default function App() {
return <MyLayout><Outlet /></MyLayout>;
}
export function HydrateFallback() {
return <MyLayout><p>Loading...</p></MyLayout>;
} So that tells me it has something to do with the way |
Yea after making this issue I added a This way I get the benefits from prerendering since my initial page contains the nav bar (unfortunately with the "home" I think it would make sense if during prerendering the |
I'm using React Router as a...
framework
Reproduction
https://stackblitz.com/edit/github-7pxkksy3?file=app%2Froot.tsx
Project setup
root.tsx
routes.ts
app.css
routes/home.tsx
routes/other.tsx
react-router.config.ts
Steps to reproduce
react-router build
in the terminalbuild/client
with a webservercd ./build/client
miniserve --spa --index index.html
(using https://github.com/svenstaro/miniserve as an example but can use whatever server for SPA)localhost:8080
,NavLink
for "home" should be active (orange background)NavLink
for "other", this navigates and makes it activeNavLink
for "home" is active again even though we're looking at the "other" pagelocalhost:8080/other
directlySystem Info
Used Package Manager
npm
Expected Behavior
When serving the
build/client
directory with a server and directly visitinglocalhost:8080/other
theNavLink
for "other" should be active.Actual Behavior
When directly visiting
localhost:8080/other
theNavLink
for "home" is active.Note that if you have multiple
NavLink
"home" stays active until you visit the home route and navigate away again. Navigating to any other route while "home" is active will show two activeNavLink
s.Workaround
Moving the whole
<nav>
into theroutes/home.tsx
androutes/other.tsx
directly seems to fix the issue, when reloading the page the correctNavLink
becomes active.Potential cause
When looking at
build/client/index.html
we can see that theNavLinks
have been pre-rendered (as described in the docs https://reactrouter.com/how-to/spa#important-note) and the "home"NavLink
has been pre-rendered withclass="active"
.Somehow after hydration this active class should switch to the correct
NavLink
but it doesn't. Interestingly there are no hydration warnings from React in the console.Potential solutions
This could very well be me "misusing"
NavLink
s (that they are not supposed to be rendered in<Layout />
), in which case I hope I can be pointed to the right docs or that I can help update the documentation to make this behaviour clearer.Otherwise I see two strategies:
NavLink
s should pre-render without their active classNavLinks
should rerender to show the active classThe text was updated successfully, but these errors were encountered: