Skip to content

Commit 1c6a2e9

Browse files
committed
update guides (end)
1 parent ee2049d commit 1c6a2e9

File tree

4 files changed

+165
-245
lines changed

4 files changed

+165
-245
lines changed

docs_headless/src/content/docs/guides/Routing.md

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
---
2-
layout: default
3-
title: "Routing in React-Admin Apps"
2+
title: "Routing"
43
---
54

6-
# Routing
7-
8-
React-admin uses [the react-router library](https://reactrouter.com/) to handle routing. This allows to use different routing strategies, and to integrate a react-admin app inside another app.
5+
Ra-core uses [the react-router library](https://reactrouter.com/) to handle routing. This allows to use different routing strategies, and to integrate an ra-core app inside another app.
96

107
## Route Structure
118

12-
For each `<Resource>`, react-admin creates 4 routes:
9+
For each `<Resource>`, ra-core creates 4 routes:
1310

1411
* `/:resource`: the list page
1512
* `/:resource/create`: the create page
1613
* `/:resource/:id/edit`: the edit page
1714
* `/:resource/:id/show`: the show page
1815

19-
These routes are fixed (i.e. they cannot be changed via configuration). Having constant routing rules allow react-admin to handle cross-resource links natively.
16+
These routes are fixed (i.e. they cannot be changed via configuration). Having constant routing rules allow ra-core to handle cross-resource links natively.
2017

21-
**Tip**: React-admin allows to use resource names containing slashes, e.g. 'cms/categories'.
18+
**Tip**: Ra-core allows to use resource names containing slashes, e.g. 'cms/categories'.
2219

2320
## Linking To A Page
2421

@@ -37,11 +34,11 @@ const Dashboard = () => (
3734
);
3835
```
3936

40-
Internally, react-admin uses a helper to build links, to allow mounting react-admin apps inside an existing app. You can use this helper, `useCreatePath`, in your components, if they have to work in admins mounted in a sub path:
37+
Internally, ra-core uses a helper to build links, to allow mounting ra-core apps inside an existing app. You can use this helper, `useCreatePath`, in your components, if they have to work in admins mounted in a sub path:
4138

4239
```jsx
4340
import { Link } from 'react-router-dom';
44-
import { useCreatePath } from 'react-admin';
41+
import { useCreatePath } from 'ra-core';
4542

4643
const Dashboard = () => {
4744
const createPath = useCreatePath();
@@ -76,16 +73,14 @@ export const usePageTracking = () => {
7673
}
7774
```
7875

79-
Then, use that hook in a [custom layout](./Admin.md#layout):
76+
Then, use that hook in your [layout](../app-configuration/CoreAdmin.md#layout):
8077

8178
```jsx
82-
import { Layout } from 'react-admin';
83-
8479
import { usePageTracking } from './usePageTracking';
8580

8681
export const MyLayout = ({ children }) => {
8782
usePageTracking();
88-
return <Layout>{children}</Layout>;
83+
return <div className="admin-layout">{children}</div>;
8984
}
9085
```
9186

@@ -97,66 +92,66 @@ export const MyLayout = ({ children }) => {
9792

9893
## Adding Custom Pages
9994

100-
In addition to CRUD pages for resources, you can create as many routes as you want for your custom pages. Use [the `<CustomRoutes>` component](./CustomRoutes.md) to do so.
95+
In addition to CRUD pages for resources, you can create as many routes as you want for your custom pages. Use [the `<CustomRoutes>` component](../app-configuration/CustomRoutes.md) to do so.
10196

10297
```jsx
10398
// in src/App.js
10499
import * as React from "react";
105100
import { Route } from 'react-router-dom';
106-
import { Admin, Resource, CustomRoutes } from 'react-admin';
101+
import { CoreAdmin, Resource, CustomRoutes } from 'ra-core';
107102
import posts from './posts';
108103
import comments from './comments';
109104
import Settings from './Settings';
110105
import Profile from './Profile';
111106

112107
const App = () => (
113-
<Admin dataProvider={simpleRestProvider('http://path.to.my.api')}>
108+
<CoreAdmin dataProvider={simpleRestProvider('http://path.to.my.api')}>
114109
<Resource name="posts" {...posts} />
115110
<Resource name="comments" {...comments} />
116111
<CustomRoutes>
117112
<Route path="/settings" element={<Settings />} />
118113
<Route path="/profile" element={<Profile />} />
119114
</CustomRoutes>
120-
</Admin>
115+
</CoreAdmin>
121116
);
122117

123118
export default App;
124119
```
125120

126121
## Using A Custom Router
127122

128-
React-admin uses [the react-router library](https://reactrouter.com/) to handle routing, with a [HashRouter](https://reactrouter.com/en/routers/create-hash-router). This means that the hash portion of the URL (i.e. `#/posts/123` in the example) contains the main application route. This strategy has the benefit of working without a server, and with legacy web browsers.
123+
Ra-core uses [the react-router library](https://reactrouter.com/) to handle routing, with a [HashRouter](https://reactrouter.com/en/routers/create-hash-router). This means that the hash portion of the URL (i.e. `#/posts/123` in the example) contains the main application route. This strategy has the benefit of working without a server, and with legacy web browsers.
129124

130-
But you may want to use another routing strategy, e.g. to allow server-side rendering of individual pages. React-router offers various Router components to implement such routing strategies. If you want to use a different router, simply put your app in a create router function. React-admin will detect that it's already inside a router, and skip its own router.
125+
But you may want to use another routing strategy, e.g. to allow server-side rendering of individual pages. React-router offers various Router components to implement such routing strategies. If you want to use a different router, simply put your app in a create router function. Ra-core will detect that it's already inside a router, and skip its own router.
131126

132127
```tsx
133128
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
134-
import { Admin, Resource } from 'react-admin';
129+
import { CoreAdmin, Resource } from 'ra-core';
135130
import { dataProvider } from './dataProvider';
136131

137132
const App = () => {
138133
const router = createBrowserRouter([
139134
{
140135
path: "*",
141136
element: (
142-
<Admin dataProvider={dataProvider}>
137+
<CoreAdmin dataProvider={dataProvider}>
143138
<Resource name="posts" />
144-
</Admin>
139+
</CoreAdmin>
145140
),
146141
},
147142
]);
148143
return <RouterProvider router={router} />;
149144
};
150145
```
151146

152-
## Using React-Admin In A Sub Path
147+
## Using Ra-Core In A Sub Path
153148

154-
React-admin links are absolute (e.g. `/posts/123/show`). If you serve your admin from a sub path (e.g. `/admin`), react-admin works seamlessly as it only appends a hash (URLs will look like `/admin#/posts/123/show`).
149+
Ra-core links are absolute (e.g. `/posts/123/show`). If you serve your admin from a sub path (e.g. `/admin`), ra-core works seamlessly as it only appends a hash (URLs will look like `/admin#/posts/123/show`).
155150

156-
However, if you serve your admin from a sub path AND use another Router (like [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) for instance), you need to set the [`opts.basename`](https://reactrouter.com/en/main/routers/create-browser-router#optsbasename) of `createBrowserRouter` function, so that react-admin routes include the basename in all links (e.g. `/admin/posts/123/show`).
151+
However, if you serve your admin from a sub path AND use another Router (like [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) for instance), you need to set the [`opts.basename`](https://reactrouter.com/en/main/routers/create-browser-router#optsbasename) of `createBrowserRouter` function, so that ra-core routes include the basename in all links (e.g. `/admin/posts/123/show`).
157152

158153
```tsx
159-
import { Admin, Resource } from 'react-admin';
154+
import { CoreAdmin, Resource } from 'ra-core';
160155
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
161156
import { dataProvider } from './dataProvider';
162157

@@ -166,9 +161,9 @@ const App = () => {
166161
{
167162
path: "*",
168163
element: (
169-
<Admin dataProvider={dataProvider}>
164+
<CoreAdmin dataProvider={dataProvider}>
170165
<Resource name="posts" />
171-
</Admin>
166+
</CoreAdmin>
172167
),
173168
},
174169
],
@@ -182,11 +177,11 @@ This makes all links be prefixed with `/admin`.
182177

183178
Note that it is your responsibility to serve the admin from the sub path, e.g. by setting the `base` field in `vite.config.ts` if you use [Vite.js](https://vitejs.dev/config/shared-options.html#base), or the `homepage` field in `package.json` if you use [Create React App](https://create-react-app.dev/docs/deployment/#building-for-relative-paths).
184179

185-
If you want to use react-admin as a sub path of a larger React application, check the next section for instructions.
180+
If you want to use ra-core as a sub path of a larger React application, check the next section for instructions.
186181

187-
## Using React-Admin Inside a Route
182+
## Using Ra-Core Inside a Route
188183

189-
You can include a react-admin app inside another app, using a react-router `<Route>`:
184+
You can include an ra-core app inside another app, using a react-router `<Route>`:
190185

191186
```tsx
192187
import { RouterProvider, Routes, Route, createBrowserRouter } from 'react-router-dom';
@@ -211,22 +206,22 @@ export const App = () => {
211206
};
212207
```
213208

214-
React-admin will have to prefix all the internal links with `/admin`. Use the `<Admin basename>` prop for that:
209+
Ra-core will have to prefix all the internal links with `/admin`. Use the `<CoreAdmin basename>` prop for that:
215210

216211
```tsx
217212
// in src/StoreAdmin.js
218-
import { Admin, Resource } from 'react-admin';
213+
import { CoreAdmin, Resource } from 'ra-core';
219214
import { dataProvider } from './dataProvider';
220215
import posts from './posts';
221216

222217
export const StoreAdmin = () => (
223-
<Admin basename="/admin" dataProvider={dataProvider}>
218+
<CoreAdmin basename="/admin" dataProvider={dataProvider}>
224219
<Resource name="posts" {...posts} />
225-
</Admin>
220+
</CoreAdmin>
226221
);
227222
```
228223

229-
This will let react-admin build absolute URLs including the sub path.
224+
This will let ra-core build absolute URLs including the sub path.
230225

231226
## Troubleshooting
232227

@@ -250,11 +245,11 @@ or
250245

251246
> `<Route>` may be used only in the context of a `<Router>` component
252247
253-
These errors can happen if you added `react-router` and/or `react-router-dom` to your dependencies, and didn't use the same version as react-admin. In that case, your application has two versions of react-router, and the calls you add can't see the react-admin routing context.
248+
These errors can happen if you added `react-router` and/or `react-router-dom` to your dependencies, and didn't use the same version as ra-core. In that case, your application has two versions of react-router, and the calls you add can't see the ra-core routing context.
254249

255250
You can use the `npm list react-router` and `npm list react-router-dom` commands to check which versions are installed.
256251

257-
If there are duplicates, you need to make sure to use only the same version as react-admin. You can deduplicate them using yarn's `resolutions` or npm's `overrides`.
252+
If there are duplicates, you need to make sure to use only the same version as ra-core. You can deduplicate them using yarn's `resolutions` or npm's `overrides`.
258253

259254
```js
260255
// in packages.json
@@ -267,4 +262,3 @@ If there are duplicates, you need to make sure to use only the same version as r
267262
}
268263
```
269264

270-
This may also happen inside a [Remix](https://remix.run/) application. See [Setting up react-admin for Remix](./Remix.md#setting-up-react-admin-in-remix) for instructions to overcome that problem.

0 commit comments

Comments
 (0)