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

Mocked example of Link without "from" behavior change #3106

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Link, type LinkProps } from '@tanstack/react-router';

export function LinkWrapper({ className, children, search, to }: { className?: string; children: React.ReactNode; to: string, search: (prev: Record<string, string>) => Record<string, string> }) {
return <Link rel="noreferrer" preload="intent" to={to} search={search} className={className}>{children}</Link>
}

export function LinkWrapperWithFrom({ className, children, from, search, to }: { className?: string; children: React.ReactNode; to: string, from?: LinkProps['from'], search: (prev: Record<string, string>) => Record<string, string> }) {
return <Link rel="noreferrer" preload="intent" to={to} search={search} from={from} className={className}>{children}</Link>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'
import { Link, Outlet, createLazyFileRoute } from '@tanstack/react-router'
import { LinkWrapper, LinkWrapperWithFrom } from '../linkWrapper'

export const Route = createLazyFileRoute('/_layout-test')({
component: LayoutComponent,
Expand All @@ -10,6 +11,8 @@ function LayoutComponent() {
<div>
<div>I'm a layout</div>
<div className="flex gap-2">
<LinkWrapper to="." search={(prev) => ({ ...prev, test: 'test' })} className="text-red-500">Doesn't Work</LinkWrapper>
<LinkWrapperWithFrom to="." from={undefined} search={(prev) => ({ ...prev, test: 'test' })} className="text-green-500">Works</LinkWrapperWithFrom>
<Link
to="/layout-a"
activeProps={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'
import { LinkWrapper } from '../../linkWrapper'

export const Route = createFileRoute('/_layout-test/layout-a')({
component: LayoutAComponent,
})

function LayoutAComponent() {
return <div>I'm A!</div>
return (
<div>
I'm A!
{/* <div>
<LinkWrapper to="." search={(prev) => ({ ...prev, test: 'test' })} className="text-green-500">Wrapper Test (Works as Expected)</LinkWrapper>
</div> */}
</div>
)
}
37 changes: 33 additions & 4 deletions examples/react/quickstart-file-based/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@
// You should NOT make any changes in this file as it will be overwritten.
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.

import { createFileRoute } from '@tanstack/react-router'

// Import Routes

import { Route as rootRoute } from './routes/__root'
import { Route as AboutImport } from './routes/about'
import { Route as IndexImport } from './routes/index'

// Create Virtual Routes

const WorkingpreloadLazyImport = createFileRoute('/workingpreload')()

// Create/Update Routes

const WorkingpreloadLazyRoute = WorkingpreloadLazyImport.update({
path: '/workingpreload',
getParentRoute: () => rootRoute,
} as any).lazy(() =>
import('./routes/workingpreload.lazy').then((d) => d.Route),
)

const AboutRoute = AboutImport.update({
id: '/about',
path: '/about',
Expand Down Expand Up @@ -46,6 +59,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof AboutImport
parentRoute: typeof rootRoute
}
'/workingpreload': {
id: '/workingpreload'
path: '/workingpreload'
fullPath: '/workingpreload'
preLoaderRoute: typeof WorkingpreloadLazyImport
parentRoute: typeof rootRoute
}
}
}

Expand All @@ -54,36 +74,41 @@ declare module '@tanstack/react-router' {
export interface FileRoutesByFullPath {
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/workingpreload': typeof WorkingpreloadLazyRoute
}

export interface FileRoutesByTo {
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/workingpreload': typeof WorkingpreloadLazyRoute
}

export interface FileRoutesById {
__root__: typeof rootRoute
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/workingpreload': typeof WorkingpreloadLazyRoute
}

export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '/about'
fullPaths: '/' | '/about' | '/workingpreload'
fileRoutesByTo: FileRoutesByTo
to: '/' | '/about'
id: '__root__' | '/' | '/about'
to: '/' | '/about' | '/workingpreload'
id: '__root__' | '/' | '/about' | '/workingpreload'
fileRoutesById: FileRoutesById
}

export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
AboutRoute: typeof AboutRoute
WorkingpreloadLazyRoute: typeof WorkingpreloadLazyRoute
}

const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
AboutRoute: AboutRoute,
WorkingpreloadLazyRoute: WorkingpreloadLazyRoute,
}

export const routeTree = rootRoute
Expand All @@ -97,14 +122,18 @@ export const routeTree = rootRoute
"filePath": "__root.tsx",
"children": [
"/",
"/about"
"/about",
"/workingpreload"
]
},
"/": {
"filePath": "index.tsx"
},
"/about": {
"filePath": "about.tsx"
},
"/workingpreload": {
"filePath": "workingpreload.lazy.tsx"
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions examples/react/quickstart-file-based/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ function RootComponent() {
}}
>
About
</Link>{' '}
<Link
to="/workingpreload"
activeProps={{
className: 'font-bold',
}}
>
Hover me!
</Link>
</div>
<hr />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react'
import { createLazyFileRoute } from '@tanstack/react-router'

export const Route = createLazyFileRoute('/workingpreload')({
component: WorkingPreloadComponent,
})

function WorkingPreloadComponent() {
return (
<div className="p-2">
<h3>Working Preload</h3>
</div>
)
}
2 changes: 1 addition & 1 deletion examples/react/quickstart-file-based/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { TanStackRouterVite } from '@tanstack/router-plugin/vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [TanStackRouterVite(), react()],
plugins: [TanStackRouterVite({ autoCodeSplitting: true }), react()],
})