Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

createRouteConfig.md

143 lines5.4 KB

1---
2id: createRouteConfig
3title: createRouteConfig
4---
5​
6Use the 'createRouteConfig' to create route config to pass to your Route Provider.
7```tsx
8import { createRouteConfig } from '@tanstack/react-router'
9​
10const router = createRouteConfig()
11```
12'createRouteConfig' takes the following options:
13​
14**Options**
15- `options: RouterOptions<TRouteConfig, TRouterContext>`
16 - **Required**
17​
18 - `action: ActionFn<>`
19​
20 An asynchronous function made available to the route for performing asynchronous or mutative actions that might invalidate the route's data.
21 - `beforeLoad:`
22​
23 This async function is called before a route is loaded.
24 If an error is thrown here, the route's loader will not be called.
25 If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onLoadError` function.
26 If thrown during a preload event, the error will be logged to the console.
28 - `caseSensitive: boolean`
29​
30 'caseSensitive' takes a boolean `true | false` , which defines if you want your routes to be case sensitive or not.
31 - `component: RouteComponent<{}>`
32​
33 This takes in a JSX component which you want to render for the particular route path:
34​
35 ```tsx
36 const rootRouter = createRouteConfig({
37 component: () => {
38 return (
39 <div>
40 // your data here
41 </div>
42 )
43 }
44​
45 // you can also pass in the component like this
46 // component: Home
47 })
48 - `errorComponent:RouteComponent<{error, info}>`
49​
50 This renders when the route has resulted in an error:
51 ```tsx
52 const rootRouter = createRouteConfig({
53 errorComponent: (error, info) => {
54 return (
55 <div>
56 // your data here
57 <h1>{error}</h1>
58 <h1>{info}</h1>
59 </div>
60 )
61 }
62 })
63 - `loader: LoaderFn<AnyLoaderData, {}, {}>`
64​
65 This is used when you want you load data in your route which can later be read using 'useLoaderData' or 'useMatch'
66​
67 ```tsx
68 const rootRouter = createRouteConfig({
69 loader: async () => {
70 const posts = await axios
71 .get<PostType[]>('https://jsonplaceholder.typicode.com/posts')
72 .then((r) => r.data.slice(0, 10));
73​
74 return { posts };
75 }
76 })
77 - `loaderGcMaxAge: number`
78​
79 The max age to cache the loader data for this route in milliseconds from the time of route inactivity before it is garbage collected.
80 - `loaderMaxAge: number`
81​
82 The max age to consider loader data fresh (not-stale) for this route in milliseconds from the time of fetch
83 Defaults to 0. Only stale loader data is refetched.
84 - `meta: RouteMeta`
86 An object of whatever you want! This object is accessible anywhere matches are.
87 - `onLoadError: ((err: any) => void) | undefined`
89 This function will be called if the route's loader throws an error **during an attempted navigation**.
90 If you want to redirect due to an error, call `router.navigate()` from within this function.
91 ```tsx
92 const rootRouter = createRouteConfig({
93 onLoadError: (err) => {
94 //redirect using router.navigate()
95 router.navigate('/')
96 }
97 })
98 - `onLoaded: ((matchContext: {params: {},
99 search: {}}) => void | ((match: {params: {};
100 search: {};
101 }) => void) | undefined) | undefined `
103 This function is called when moving from an inactive state to an active one. Likewise, when moving from an active to an inactive state, the return function (if provided) is called.
105 - `onTransition: ((match: {
106 params: {};
107 search: {};
108 }) => void) | undefined `
110 This function is called when the route remains active from one transition to the next.
111​
112 - `parseParams: ((rawParams: Record<never, string>) => Record<never, string>) | undefined`
114 Parse params optionally receives path params as strings and returns them in a parsed format (like a number or boolean)
116 - `path:`
117​
118 The path to match (relative to the nearest parent `Route` component or root basepath)
119​
121 - `pendingComponent: RouteComponenet<{}>`
122​
123 the content to be rendered as the fallback content until the route is ready to render
124​
126 - `postSearchFilters:SearchFilter<{}, {}>[]`
127​
128 Filter functions that can manipulate search params *after* they are passed to links and navigate calls that match this route.
130 - `preSearchFilters:SearchFilter<{}, {}>[]`
131​
132 Filter functions that can manipulate search params *before* they are passed to links and navigate calls that match this route.
134 - `stringifyParams:((params: Record<never, string>) => Record<never, string>) | undefined`
135​
136 stringify params optionally receives path params and returns them in a string format
138 - `validateSearch:SearchSchemaValidator<AnySearchSchema, {}> | undefined`
139​
140 validateSearch takes in the validation schema to validate the search params , you can also pass zod validation schema as a validator.
141​
142**Returns**
143- `router: Router<TRouteConfig, TAllRouteInfo, TRouterContext>`