-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(react-router): useMatchRoute w/ React Compiler #8015
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
Open
Sheraff
wants to merge
6
commits into
main
Choose a base branch
from
fix-react-router-use-match-route-react-compiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6924573
fix(react-router): useMatchRoute w/ React Compiler
Sheraff a3f68ce
test(react-router): add React Compiler E2E
Sheraff 740dc3b
disable eslint react-hooks/exhaustive-deps for known static condition…
Sheraff 67802df
test(react-router): cover useMatchRoute callback identity
Sheraff 38c601d
test(react-router): rely on useMatchRoute subscription
Sheraff f1fffd1
test(react-router): exercise compiler-generated memoization
Sheraff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>React Compiler useMatchRoute test</title> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="/src/main.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "tanstack-router-e2e-react-compiler", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite --port 3000", | ||
| "dev:e2e": "vite", | ||
| "build": "vite build && tsc --noEmit", | ||
| "preview": "vite preview", | ||
| "start": "vite", | ||
| "test:e2e": "rm -rf port*.txt; playwright test --project=chromium" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/react-router": "workspace:^", | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/core": "^7.29.0", | ||
| "@playwright/test": "^1.61.0", | ||
| "@rolldown/plugin-babel": "^0.2.0", | ||
| "@tanstack/router-e2e-utils": "workspace:^", | ||
| "@types/react": "^19.0.8", | ||
| "@types/react-dom": "^19.0.3", | ||
| "@vitejs/plugin-react": "^6.0.1", | ||
| "babel-plugin-react-compiler": "^1.0.0", | ||
| "vite": "^8.0.14" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { defineConfig, devices } from '@playwright/test' | ||
| import { getTestServerPort } from '@tanstack/router-e2e-utils' | ||
| import packageJson from './package.json' with { type: 'json' } | ||
|
|
||
| const PORT = await getTestServerPort(packageJson.name) | ||
| const baseURL = `http://localhost:${PORT}` | ||
|
|
||
| export default defineConfig({ | ||
| testDir: './tests', | ||
| workers: 1, | ||
| reporter: [['line']], | ||
| use: { baseURL }, | ||
| webServer: { | ||
| command: `VITE_NODE_ENV="test" VITE_SERVER_PORT=${PORT} pnpm dev:e2e --port ${PORT}`, | ||
| url: baseURL, | ||
| reuseExistingServer: !process.env.CI, | ||
| stdout: 'pipe', | ||
| }, | ||
| projects: [ | ||
| { | ||
| name: 'chromium', | ||
| use: { ...devices['Desktop Chrome'] }, | ||
| }, | ||
| ], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { StrictMode } from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
| import { | ||
| Link, | ||
| Outlet, | ||
| RouterProvider, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| linkOptions, | ||
| useMatchRoute, | ||
| } from '@tanstack/react-router' | ||
|
|
||
| const links = linkOptions([ | ||
| { to: '/home', label: 'Home' }, | ||
| { to: '/about', label: 'About' }, | ||
| ]) | ||
|
|
||
| function useRouteName() { | ||
| const matchRoute = useMatchRoute() | ||
|
|
||
| return links.find((link) => matchRoute(link))?.label ?? 'Unknown' | ||
| } | ||
|
|
||
| function RootComponent() { | ||
| const matchedRoute = useRouteName() | ||
|
|
||
| return ( | ||
| <> | ||
| <nav> | ||
| {links.map((link) => ( | ||
| <Link key={link.label} {...link}> | ||
| {link.label} | ||
| </Link> | ||
| ))} | ||
| </nav> | ||
| <p> | ||
| Matched route: <span data-testid="matched-route">{matchedRoute}</span> | ||
| </p> | ||
| <Outlet /> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| const rootRoute = createRootRoute({ component: RootComponent }) | ||
| const homeRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/home', | ||
| }) | ||
| const aboutRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/about', | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([homeRoute, aboutRoute]), | ||
| }) | ||
|
|
||
| declare module '@tanstack/react-router' { | ||
| interface Register { | ||
| router: typeof router | ||
| } | ||
| } | ||
|
|
||
| const rootElement = document.getElementById('app') | ||
| if (!rootElement) { | ||
| throw new Error('Root element not found') | ||
| } | ||
|
|
||
| createRoot(rootElement).render( | ||
| <StrictMode> | ||
| <RouterProvider router={router} /> | ||
| </StrictMode>, | ||
| ) |
16 changes: 16 additions & 0 deletions
16
e2e/react-router/react-compiler/tests/use-match-route.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { expect, test } from '@playwright/test' | ||
|
|
||
| test('useMatchRoute updates after navigation with React Compiler', async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto('/home') | ||
| await expect(page.getByTestId('matched-route')).toHaveText('Home') | ||
|
|
||
| await page.getByRole('link', { name: 'About' }).click() | ||
| await expect(page).toHaveURL(/\/about$/) | ||
| await expect(page.getByTestId('matched-route')).toHaveText('About') | ||
|
|
||
| await page.getByRole('link', { name: 'Home' }).click() | ||
| await expect(page).toHaveURL(/\/home$/) | ||
| await expect(page.getByTestId('matched-route')).toHaveText('Home') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "jsx": "react-jsx", | ||
| "target": "ESNext", | ||
| "moduleResolution": "Bundler", | ||
| "module": "ESNext", | ||
| "resolveJsonModule": true, | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "types": ["vite/client"] | ||
| }, | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineConfig } from 'vite' | ||
| import react, { reactCompilerPreset } from '@vitejs/plugin-react' | ||
| import babel from '@rolldown/plugin-babel' | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [react(), babel({ presets: [reactCompilerPreset()] })], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.