Open History to see the last commit
1import {
2 Link,
3 Outlet,
4 RouterProvider,
5 createRootRoute,
6 createRoute,
7 createRouter,
8 useBlocker,
9} from '@tanstack/react-router'
10import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
11import React from 'react'
12import ReactDOM from 'react-dom/client'
13import './styles.css'
14β
15const rootRoute = createRootRoute({
16 component: RootComponent,
17})
18β
19function RootComponent() {
20 // block going from editor-1 to /foo/123?hello=world
21 const { proceed, reset, status } = useBlocker({
22 shouldBlockFn: ({ current, next }) => {
23 if (
24 current.routeId === '/editor-1' &&
25 next.fullPath === '/foo/$id' &&
26 next.params.id === '123' &&
27 next.search.hello === 'world'
28 ) {
29 return true
30 }
31 return false
32 },
33 enableBeforeUnload: false,
34 withResolver: true,
35 })
36β
37 return (
38 <>
39 <div className="p-2 flex gap-2 text-lg">
40 <Link
41 to="/"
42 activeProps={{
43 className: 'font-bold',
44 }}
45 activeOptions={{ exact: true }}
46 >
47 Home
48 </Link>{' '}
49 <Link
50 to="/editor-1"
51 activeProps={{
52 className: 'font-bold',
53 }}
54 >
55 Editor 1
56 </Link>{' '}
57 <Link
58 to={'/editor-1/editor-2'}
59 activeProps={{
60 className: 'font-bold',
61 }}
62 >
63 Editor 2
64 </Link>{' '}
65 <Link
66 to="/foo/$id"
67 params={{ id: '123' }}
68 search={{ hello: 'world' }}
69 activeProps={{
70 className: 'font-bold',
71 }}
72 activeOptions={{ exact: true, includeSearch: true }}
73 >
74 foo 123
75 </Link>{' '}
76 <Link
77 to="/foo/$id"
78 params={{ id: '456' }}
79 search={{ hello: 'universe' }}
80 activeProps={{
81 className: 'font-bold',
82 }}
83 activeOptions={{ exact: true, includeSearch: true }}
84 >
85 foo 456
86 </Link>{' '}
87 </div>
88 <hr />
89β
90 {status === 'blocked' && (
91 <div className="mt-2">
92 <div>
93 Are you sure you want to leave editor 1 for /foo/123?hello=world ?
94 </div>
95 <button
96 className="bg-lime-500 text-white rounded-sm p-1 px-2 mr-2"
97 onClick={proceed}
98 >
99 YES
100 </button>
101 <button
102 className="bg-red-500 text-white rounded-sm p-1 px-2"
103 onClick={reset}
104 >
105 NO
106 </button>
107 </div>
108 )}
109 <Outlet />
110 <TanStackRouterDevtools position="bottom-right" />
111 </>
112 )
113}
114β
115const indexRoute = createRoute({
116 getParentRoute: () => rootRoute,
117 path: '/',
118 component: IndexComponent,
119})
120β
121function IndexComponent() {
122 return (
123 <div className="p-2">
124 <h3>Welcome Home!</h3>
125 </div>
126 )
127}
128β
129const fooRoute = createRoute({
130 getParentRoute: () => rootRoute,
131 path: 'foo/$id',
132 validateSearch: (search) => ({ hello: search.hello }) as { hello: string },
133 component: () => <>foo {fooRoute.useParams().id}</>,
134})
135β
136const editor1Route = createRoute({
137 getParentRoute: () => rootRoute,
138 path: 'editor-1',
139 component: Editor1Component,
140})
141β
142function Editor1Component() {
143 const [value, setValue] = React.useState('')
144β
145 // Block leaving editor-1 if there is text in the input
146 const { proceed, reset, next, current, status } = useBlocker({
147 shouldBlockFn: () => value !== '',
148 enableBeforeUnload: () => value !== '',
149 withResolver: true,
150 })
151β
152 return (
153 <div className="flex flex-col p-2">
154 <h3>Editor 1</h3>
155 <div>
156 <input
157 value={value}
158 onChange={(e) => setValue(e.target.value)}
159 className="border"
160 />
161 </div>
162 <hr className="m-2" />
163 <Link to="/editor-1/editor-2">Go to Editor 2</Link>
164 <Outlet />
165β
166 {status === 'blocked' && (
167 <div className="mt-2">
168 <div>Are you sure you want to leave editor 1?</div>
169 <div>
170 You are going from {current.pathname} to {next.pathname}
171 </div>
172 <button
173 className="bg-lime-500 text-white rounded-sm p-1 px-2 mr-2"
174 onClick={proceed}
175 >
176 YES
177 </button>
178 <button
179 className="bg-red-500 text-white rounded-sm p-1 px-2"
180 onClick={reset}
181 >
182 NO
183 </button>
184 </div>
185 )}
186 </div>
187 )
188}
189β
190const editor2Route = createRoute({
191 getParentRoute: () => editor1Route,
192 path: 'editor-2',
193 component: Editor2Component,
194})
195β
196function Editor2Component() {
197 const [value, setValue] = React.useState('')
198β
199 return (
200 <div className="p-2">
201 <h3>Editor 2</h3>
202 <input
203 value={value}
204 onChange={(e) => setValue(e.target.value)}
205 className="border"
206 />
207 </div>
208 )
209}
210β
211const routeTree = rootRoute.addChildren([
212 indexRoute,
213 fooRoute,
214 editor1Route.addChildren([editor2Route]),
215])
216β
217// Set up a Router instance
218const router = createRouter({
219 routeTree,
220 defaultPreload: 'intent',
221 scrollRestoration: true,
222})
223β
224const rootElement = document.getElementById('app')!
225β
226if (!rootElement.innerHTML) {
227 const root = ReactDOM.createRoot(rootElement)
228β
229 root.render(<RouterProvider router={router} />)
230}