TanStack / TanStack/router

Unexpected re-render caused by `MatchRoute`

Open
#4,034 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

MatchRoute will an unexpected error, if the child component of MatchRoute is a function:

// unexpected re-render
<MatchRoute to="/posts/$postId" params={{ postId: '1' }}>
    {(matches) => {
      return matches && <Post1Component />
    }}
</MatchRoute>
// expected behavior
<MatchRoute to="/posts/$postId" params={{ postId: '2' }}>
   <Post2Component />
</MatchRoute>

If you are at /posts/1, then go to /posts/2. An unexpected re-render of <Post1Component /> will happen.

Your Example Website or App

https://stackblitz.com/~/github.com/gz65555/tanstack-unexpected-rerender

Steps to Reproduce the Bug or Issue
  1. Go to the reproduce url
  2. click "Posts"
  3. click "Post1"
  4. click "Post2"

You can see the bug from devtools:

Image

Expected behavior

I expected that the console should be:

render post1
render post2
Platform
  • OS: macOS
  • Browser: chrome
  • Version: 133

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and follow the listed navigation steps: Posts, Post1, then Post2. Inspect MatchRoute behavior with a function child using the browser devtools. Done means navigating from post 1 to post 2 logs only “render post1” and “render post2”, without an unexpected Post1Component re-render.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.