|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "React-admin With TanStack Start" |
| 4 | +--- |
| 5 | + |
| 6 | +# React-admin With TanStack Start |
| 7 | + |
| 8 | +React-admin supports [TanStack Router](https://tanstack.com/router/latest) as an alternative to react-router. This allows you to use react-admin in a TanStack Start application. |
| 9 | + |
| 10 | +## Setting Up TanStack Start |
| 11 | + |
| 12 | +Let's start by creating a new TanStack Start project. Run the following command: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm create @tanstack/start@latest |
| 16 | +``` |
| 17 | + |
| 18 | +This script will ask you for more details about your project. Follow the prompts, then move into the project directory. |
| 19 | + |
| 20 | +## Setting Up React-Admin In TanStack Start |
| 21 | + |
| 22 | +Add the `react-admin` and `ra-router-tanstack` packages, 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). |
| 23 | + |
| 24 | +```bash |
| 25 | +cd tanstack-start-admin |
| 26 | +npm install react-admin ra-router-tanstack ra-data-json-server |
| 27 | +# or |
| 28 | +yarn add react-admin ra-router-tanstack ra-data-json-server |
| 29 | +``` |
| 30 | + |
| 31 | +## Adding React-Admin In A Route |
| 32 | + |
| 33 | +Create a new route at `/admin` and render react-admin there. TanStack Start uses file-based routing, so create `src/routes/admin.tsx` (or the equivalent routes directory in your template) with the following content: |
| 34 | + |
| 35 | +```tsx |
| 36 | +// in src/routes/admin.tsx |
| 37 | +import { createFileRoute } from '@tanstack/react-router'; |
| 38 | +import { Admin, Resource, ListGuesser } from 'react-admin'; |
| 39 | +import jsonServerProvider from 'ra-data-json-server'; |
| 40 | +import { tanStackRouterProvider } from 'ra-router-tanstack'; |
| 41 | + |
| 42 | +const dataProvider = jsonServerProvider('https://jsonplaceholder.typicode.com'); |
| 43 | + |
| 44 | +export const Route = createFileRoute('/admin')({ |
| 45 | + component: AdminRoute, |
| 46 | +}); |
| 47 | + |
| 48 | +function AdminRoute() { |
| 49 | + return ( |
| 50 | + <Admin |
| 51 | + dataProvider={dataProvider} |
| 52 | + routerProvider={tanStackRouterProvider} |
| 53 | + basename="/admin" |
| 54 | + > |
| 55 | + <Resource name="posts" list={ListGuesser} /> |
| 56 | + <Resource name="comments" list={ListGuesser} /> |
| 57 | + </Admin> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +**Tip**: Don't forget to set the `<Admin basename>` prop, so that react-admin generates links relative to the `/admin` subpath. |
| 64 | + |
| 65 | +You can now start the app in `development` mode with `npm run dev`. The admin should render at `/admin` on your dev server. |
| 66 | + |
| 67 | +## Next Steps |
| 68 | + |
| 69 | +For standalone vs embedded setups, see the [TanStack Router Integration](./TanStackRouter.md). |
0 commit comments