You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -15,21 +15,21 @@ Next.js 13 proposes 2 ways to build a React project:
15
15
React-admin supports both ways.
16
16
17
17
## Create a Next.js application
18
-
Let's start by creating a new Next.js project called nextjs-react-admin.
18
+
Let's start by creating a new Next.js project called next-admin.
19
19
20
20
```bash
21
21
npx create-next-app@latest
22
22
```
23
-
A prompt will asks you some questions, feel free to choose the options according to your needs.
24
-
To easily bootstap the `<Admin>` application, regardless of Next.js router system (eg: Page Router or App router), we will choose to create a `src` folder :
23
+
A prompt will asks you some questions, feel free to choose answers according to your needs.
24
+
To easily bootstap the `<Admin>` application, regardless of Next.js router system (eg: Pages Router or App router), we will choose to create a `src` folder :
25
25
26
26

27
27
28
28
This creates a project with the following folder structure:
|||
32
+
|||
Next, create a `components` directory inside `src`, and an admin App component in `src/components/AdminApp.jsx`:
46
46
47
-
> **Tips**: If you choose App Router, do not forget to add [the `"use client"` directive](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive).
47
+
**Tips**: If you choose App Router, do not forget to add [the `"use client"` directive](https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive). Why the `"use client"` directive? React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with various client-side only libraries (emotion, material-ui, react-query) leveraging the React Context API, and cannot be rendered using React Server components.
48
48
49
49
```jsx
50
50
// in src/components/AdminApp.jsx
@@ -77,7 +77,7 @@ export default AdminApp;
77
77
78
78
This is a minimal configuration to render CRUD pages for users, posts and comments. React-admin guesses the data structure from the API response.
79
79
80
-
## Exposing the Admin App Component
80
+
###Exposing the Admin App Component
81
81
React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with its own routing sytem, which conflicts with the Next.js routing system. So we must prevent Next.js from rendering the react-admin component on the server-side.
82
82
83
83
To do that, we will have to import our `<AdminApp>` component in Next.js by using the [__lazy loading__ system provided by Next.js](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading) and specify the [`ssr` option to false](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#with-no-ssr).
@@ -87,10 +87,12 @@ Using dynamic import allows disabling Server-Side Rendering for the `<AdminApp>`
87
87
The file you import the Admin app into Next.js depends on the router system you choose :
88
88
89
89
- if you choose App Router, import `<AdminApp>` in `src/app/page.tsx`,
90
-
- otherwise, if you choose Page Router, import `<AdminApp>` in `src/pages/index.tsx`.
90
+
- otherwise, if you choose Pages Router, import `<AdminApp>` in `src/pages/index.tsx`.
91
91
92
92
```tsx
93
-
// in src/app/page.tsx or in src/pages/index.tsx depending of the Router you choose
93
+
// depending of the Router you choose:
94
+
// - in src/app/page.tsx for App Router system
95
+
// - or in src/pages/index.tsx for Pages Router system
Add the `react-admin` npm package, as well as a data provider package. In this example, we'll use `ra-data-json-server` to connect to a test API provided by [JSONPlaceholder](https://jsonplaceholder.typicode.com).
122
-
123
-
```bash
124
-
cd next-admin
125
-
yarn add react-admin ra-data-json-server
126
-
```
127
-
128
-
### Creating the Admin App Component
129
-
130
-
Next, create an `admin` directory inside `src`, and the admin App component in `src/admin/App.jsx`:
Starting from there, you can [Add an API](#adding-an-api) as described in the next section, and/or add features to the Next.js app, as explained in the [Getting started tutorial](./Tutorial.md)
This is a minimal configuration to render CRUD pages for users, posts and comments. React-admin guesses the data structure from the API response.
152
-
153
-
### Exposing the Admin App Component
154
-
155
-
Now, let's configure Next.js to render the admin app component in the root path ('/'). Edit the file called `src/pages/index.tsx`, and replace the content with the following:
Now the admin renders at `http://localhost:3000/admin`, and you can use the Next.js routing system to add more pages.
196
133
134
+
**Tip**: If you migrated from the Pages Router, you might have to delete the `.next` directory in your project to ensure NextJS bundles the client dependencies correctly.
135
+
197
136
### Adding an API
198
137
199
138
[Next.js allows to serve an API](https://nextjs.org/docs/api-routes/introduction) from the same server. You *could* use this to build a CRUD API by hand. However, we consider that building a CRUD API on top of a relational database is a solved problem and that developers shouldn't spend time reimplementing it.
Add the `react-admin` npm package, as well as a data provider package. In this example, we'll use `ra-data-json-server` to connect to a test API provided by [JSONPlaceholder](https://jsonplaceholder.typicode.com).
302
-
303
-
```bash
304
-
cd next-admin
305
-
yarn add react-admin ra-data-json-server
306
-
```
307
-
308
-
### Creating the Admin App Component
309
-
310
-
Next, create a `app/Admin.tsx` file with the following code, which initializes the react-admin app:
Before you go further, you need to specify to NextJS to not use Server Side Rendering (SSR) for this component, otherwise, NextJS output will display an error. You can do that as we explain at the beginning of this tutorial. So replace the content of `app/page.tsx` with this :
Now, start the server with `yarn dev`, browse to `http://localhost:3000/`, and you should see the working admin:
342
-
343
-

344
-
345
-
React-admin renders a CRUD for users, posts and comments, guessing the data structure from the API response.
346
-
347
-
348
-
**Tip**: Why the `"use client"` directive? React-admin is designed as a Single-Page Application, rendered on the client-side. It comes with various client-side only libraries (emotion, material-ui, react-query) leveraging the React Context API, and cannot be rendered using React Server components.
349
-
350
-
**Tip**: If you migrated from the Pages Router, you might have to delete the `.next` directory in your project to ensure NextJS bundles the client dependencies correctly.
351
-
352
-
Starting from there, you can [Add an API](#adding-an-api) as described in the previous section, and/or add features to the Next.js app, as explained in the [Getting started tutorial](./Tutorial.md)
0 commit comments