`useLocation` re-renders before navigating
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 15.1k
- Forks
- 1.9k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 143
Description
π¨ To anyone stumbling here: this is not a bug, it's an issue with the documentation. All of us migrating from react-router-dom are used to using useLocation for everything, but with tanstack/router, there are different hooks for different purposes: https://tanstack.com/router/latest/docs/framework/react/api/router
πππππ THIS IS THE SOLUTION TO YOUR PROBLEM πππππ
[!IMPORTANT]
TL;DR:
useLocationis about the URL, you most likely do not care about the URL. What you care about is probably:
- the routes =>
useMatch,useMatches,useChildMatches,useParentMatches- the params =>
useParams,useSearch
πππππ THIS IS THE SOLUTION TO YOUR PROBLEM πππππ
Which project does this relate to?
Router
Describe the bug
Assuming a simple route like this:
export const Route = createFileRoute('/about')({
component: AboutComponent,
});
function AboutComponent() {
const {pathname} = useLocation()
console.log(pathname)
return (
<div className="p-2">
<h3>About</h3>
</div>
);
}
My naive assumption would be that the only value pathname can ever have here is "/about", but in reality, this will also re-render just before navigating away, with pathname having the value of the next page.
Alternatively, we can also use the following:
const pathname = useRouterState({ select: (s) => s.resolvedLocation.pathname });
But in that case, pathname will initially have the value of the previous page before immediately re-rendering with the current page.
One way to access the pathname that seems stable is this:
const pathname = useMatch({ strict: false, select: (s) => s.pathname });
Your Example Website or App
https://stackblitz.com/edit/tanstack-router-fpv611d5?file=src%2Froutes%2F_yo%2Fabout.tsx
Steps to Reproduce the Bug or Issue
- Go to '/about' in the demo above
- open the console to see what gets logged
- Go to any other page
This will show that the console.log(pathname) (similar to the one in the code sample above) logs the next route even though it's only in the route component of /about.
The about.tsx file contains other commented-out methods of "logging the pathname" that demonstrate the various render behaviors.
Expected behavior
I'm not entirely sure what is "expected behavior" here. But my naive intuition would be that for all of these methods, since they're used inside the "/about" route, they should only ever render with the "/about" pathname.
Screenshots or Videos
https://github.com/user-attachments/assets/ee76d3b0-11eb-468d-8a4e-6cb13cacb507
Platform
- OS: macOS
- Browser: Chrome
- Version: 1.95.0
Additional context
This might be the same behavior that causes https://github.com/TanStack/router/issues/2318 because their demo uses
const location = useLocation();
<ErrorBoundary
resetKeys={[location.pathname]}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up β it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the linked router API documentation and the demo's about.tsx, starting with the useLocation, useRouterState, and useMatch examples. A useful outcome would be a decided expected behavior and a documentation update explaining which hook to use for route, params, and URL state, with the demo behavior reflected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100