|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Admin Routing Customizations`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
The Medusa Admin dashboard uses [React Router](https://reactrouter.com) under the hood to manage routing. This gives you more flexibility in routing-related customizations using React Router's utilities, hooks, and components. |
|
8
|
|
|
9
|
In this chapter, you'll learn about routing-related customizations that you can use in your widgets, UI routes, and settings pages using React Router. |
|
10
|
|
|
11
|
<Note> |
|
12
|
|
|
13
|
`react-router-dom` is available in your project by default through the Medusa packages. You don't need to install it separately. |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
<Note title="React Router v7"> |
|
18
|
|
|
19
|
Starting from [Medusa v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0), the Medusa Admin uses React Router v7. If your project has its own `react-router-dom` dependency, update it to v7 to avoid installing two versions of React Router, which leads to a `useNavigate() may be used only in the context of a Router` error in your admin customizations. |
|
20
|
|
|
21
|
React Router v7 also removed the `json` and `defer` utilities. Instead of `json`, return a `Response` object with a JSON body from your loader. Instead of `defer`, return a plain object holding the unresolved promises. |
|
22
|
|
|
23
|
</Note> |
|
24
|
|
|
25
|
## Link to a Page |
|
26
|
|
|
27
|
To link to a page in your admin customizations, you can use the `Link` component from `react-router-dom`. For example: |
|
28
|
|
|
29
|
export const highlights = [ |
|
30
|
["9", "Link", "Add a link to the orders page"], |
|
31
|
["9", '"/orders', "Add the path without the `/app` prefix."] |
|
32
|
] |
|
33
|
|
|
34
|
```tsx title="src/admin/widgets/product-widget.tsx" highlights={highlights} |
|
35
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
36
|
import { Container } from "@medusajs/ui" |
|
37
|
import { Link } from "react-router-dom" |
|
38
|
|
|
39
|
// The widget |
|
40
|
const ProductWidget = () => { |
|
41
|
return ( |
|
42
|
<Container className="divide-y p-0"> |
|
43
|
<Link to={"/orders"}>View Orders</Link> |
|
44
|
</Container> |
|
45
|
) |
|
46
|
} |
|
47
|
|
|
48
|
// The widget's configurations |
|
49
|
export const config = defineWidgetConfig({ |
|
50
|
zone: "product.details", |
|
51
|
}) |
|
52
|
|
|
53
|
export default ProductWidget |
|
54
|
``` |
|
55
|
|
|
56
|
This adds a widget to a product's details page with a link to the Orders page. The link's path must be without the `/app` prefix. |
|
57
|
|
|
58
|
--- |
|
59
|
|
|
60
|
## Fetch Data with Route Loaders |
|
61
|
|
|
62
|
<Note> |
|
63
|
|
|
64
|
Route loaders are available starting from Medusa v2.5.1. |
|
65
|
|
|
66
|
</Note> |
|
67
|
|
|
68
|
In your UI routes and settings pages, you may need to retrieve data to use in your route component. For example, you may want to fetch a list of products to display on a custom page. |
|
69
|
|
|
70
|
The recommended approach is to fetch data within the UI route component asynchronously using the JS SDK with Tanstack (React) Query as explained in [this chapter](../tips/page.mdx#send-requests-to-api-routes). |
|
71
|
|
|
72
|
However, if you need the data to be fetched before the route is rendered, such as if you're [setting breadcrumbs dynamically](../ui-routes/page.mdx#set-breadcrumbs-dynamically), you can use a route loader. |
|
73
|
|
|
74
|
To fetch data with a route loader: |
|
75
|
|
|
76
|
1. Define and export a [React Router loader](https://reactrouter.com/start/data/route-object#loader) function in the UI route's file. In this function, you can fetch and return data asynchronously. |
|
77
|
2. In your UI route's component, you can use the [useLoaderData hook from React Router](https://reactrouter.com/api/hooks/useLoaderData) to access the data returned by the `loader` function. |
|
78
|
|
|
79
|
For example, consider the following UI route created at `src/admin/routes/custom/page.tsx`: |
|
80
|
|
|
81
|
export const loaderHighlights = [ |
|
82
|
["7", "loader", "Export a loader function to fetch products."], |
|
83
|
["16", "useLoaderData", "Access the data returned by the loader function in the route component."], |
|
84
|
] |
|
85
|
|
|
86
|
```tsx title="src/admin/routes/custom/page.tsx" highlights={loaderHighlights} |
|
87
|
import { defineRouteConfig } from "@medusajs/admin-sdk" |
|
88
|
import { Container, Heading } from "@medusajs/ui" |
|
89
|
import { |
|
90
|
useLoaderData, |
|
91
|
} from "react-router-dom" |
|
92
|
|
|
93
|
export async function loader() { |
|
94
|
// TODO fetch products |
|
95
|
|
|
96
|
return { |
|
97
|
products: [], |
|
98
|
} |
|
99
|
} |
|
100
|
|
|
101
|
const CustomPage = () => { |
|
102
|
const { products } = useLoaderData() as Awaited<ReturnType<typeof loader>> |
|
103
|
|
|
104
|
return ( |
|
105
|
<div> |
|
106
|
<Container className="divide-y p-0"> |
|
107
|
<div className="flex items-center justify-between px-6 py-4"> |
|
108
|
<Heading level="h2">Products count: {products.length}</Heading> |
|
109
|
</div> |
|
110
|
</Container> |
|
111
|
</div> |
|
112
|
) |
|
113
|
} |
|
114
|
|
|
115
|
export default CustomPage |
|
116
|
``` |
|
117
|
|
|
118
|
In this example, you first export a `loader` function that can be used to fetch data, such as products. The function returns an object with a `products` property. |
|
119
|
|
|
120
|
Then, in the `CustomPage` route component, you use the `useLoaderData` hook from React Router to access the data returned by the `loader` function. You can then use the data in your component. |
|
121
|
|
|
122
|
### Route Loaders Block Rendering |
|
123
|
|
|
124
|
Route loaders block the rendering of your UI route until the data is fetched, which may negatively impact the user experience. So, only use route loaders when the route component needs essential data before rendering, or if you're preparing data that doesn't require sending API requests. |
|
125
|
|
|
126
|
Otherwise, use the JS SDK with Tanstack (React) Query in the UI route component as explained in the [Tips](../tips/page.mdx#send-requests-to-api-routes) chapter to fetch data asynchronously and update the UI when the data is available. |
|
127
|
|
|
128
|
 |
|
129
|
|
|
130
|
### Access Route Parameters in Loader |
|
131
|
|
|
132
|
You can access route parameters in the loader function. For example, consider the following UI route created at `src/admin/routes/custom/[id]/page.tsx`: |
|
133
|
|
|
134
|
export const loaderParamHighlights = [ |
|
135
|
["7", "params", "Access route parameters in the loader."] |
|
136
|
] |
|
137
|
|
|
138
|
```tsx title="src/admin/routes/custom/[id]/page.tsx" highlights={loaderParamHighlights} |
|
139
|
import { Container, Heading } from "@medusajs/ui" |
|
140
|
import { |
|
141
|
useLoaderData, |
|
142
|
LoaderFunctionArgs, |
|
143
|
} from "react-router-dom" |
|
144
|
|
|
145
|
export async function loader({ params }: LoaderFunctionArgs) { |
|
146
|
const { id } = params |
|
147
|
// TODO fetch product by id |
|
148
|
|
|
149
|
return { |
|
150
|
id, |
|
151
|
} |
|
152
|
} |
|
153
|
|
|
154
|
const CustomPage = () => { |
|
155
|
const { id } = useLoaderData() as Awaited<ReturnType<typeof loader>> |
|
156
|
|
|
157
|
return ( |
|
158
|
<div> |
|
159
|
<Container className="divide-y p-0"> |
|
160
|
<div className="flex items-center justify-between px-6 py-4"> |
|
161
|
<Heading level="h2">Product ID: {id}</Heading> |
|
162
|
</div> |
|
163
|
</Container> |
|
164
|
</div> |
|
165
|
) |
|
166
|
} |
|
167
|
|
|
168
|
export default CustomPage |
|
169
|
``` |
|
170
|
|
|
171
|
Because the UI route has a route parameter `[id]`, you can access the `id` parameter in the `loader` function. The loader function accepts as a parameter an object that has a `params` property containing the route parameters. |
|
172
|
|
|
173
|
In the loader, you can fetch data asynchronously using the route parameter and return it. Then, in the route component, you can access the data using the `useLoaderData` hook. |
|
174
|
|
|
175
|
--- |
|
176
|
|
|
177
|
## Other React Router Utilities |
|
178
|
|
|
179
|
### Route Handles |
|
180
|
|
|
181
|
<Note> |
|
182
|
|
|
183
|
Route handles are available starting from Medusa v2.5.1. |
|
184
|
|
|
185
|
</Note> |
|
186
|
|
|
187
|
In your UI route or any route file, you can export a `handle` object to define [route handles](https://reactrouter.com/start/data/route-object#handle). The object is passed to the loader and route contexts. |
|
188
|
|
|
189
|
For example: |
|
190
|
|
|
191
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
192
|
export const handle = { |
|
193
|
sandbox: true, |
|
194
|
} |
|
195
|
``` |
|
196
|
|
|
197
|
You can also use the `handle` object to define a breadcrumb for the route. Learn more in the [UI Route](../ui-routes/page.mdx) chapter. |
|
198
|
|
|
199
|
### React Router Components and Hooks |
|
200
|
|
|
201
|
Refer to [React Router’s documentation](https://reactrouter.com) for components and hooks that you can use in your admin customizations. |