|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "Vite Integration" |
| 4 | +--- |
| 5 | + |
| 6 | +# Vite Integration |
| 7 | + |
| 8 | +[Vite](https://vitejs.dev/) is a JavaScript bundler which improves speed of dev server and production build compared to Webpack. |
| 9 | + |
| 10 | +## Setting Up React App with Vite |
| 11 | + |
| 12 | +Create a new Vite project with React template using the command line: |
| 13 | + |
| 14 | +```sh |
| 15 | +yarn create vite my-admin --template react |
| 16 | +``` |
| 17 | + |
| 18 | +We recommend using the TypeScript template: |
| 19 | + |
| 20 | +```sh |
| 21 | +yarn create vite my-admin --template react-ts |
| 22 | +``` |
| 23 | + |
| 24 | +## Setting Up React-Admin |
| 25 | + |
| 26 | +Add the `react-admin` 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). |
| 27 | + |
| 28 | +```sh |
| 29 | +cd my-admin |
| 30 | +yarn add react-admin ra-data-json-server |
| 31 | +``` |
| 32 | + |
| 33 | +Next, create the admin app component in `src/admin/index.tsx`: |
| 34 | + |
| 35 | +```jsx |
| 36 | +// in src/admin/index.tsx |
| 37 | +import { Admin, Resource, ListGuesser } from "react-admin"; |
| 38 | +import jsonServerProvider from "ra-data-json-server"; |
| 39 | + |
| 40 | +const dataProvider = jsonServerProvider("https://jsonplaceholder.typicode.com"); |
| 41 | + |
| 42 | +const App = () => ( |
| 43 | + <Admin dataProvider={dataProvider}> |
| 44 | + <Resource name="posts" list={ListGuesser} /> |
| 45 | + <Resource name="comments" list={ListGuesser} /> |
| 46 | + </Admin> |
| 47 | +); |
| 48 | + |
| 49 | +export default App; |
| 50 | +``` |
| 51 | + |
| 52 | +This is a minimal admin for 2 resources. React-admin should be able to render a list of posts and a list of comments, guessing the data structure from the API response. |
| 53 | + |
| 54 | +Next, replace the `App.tsx` component with the following: |
| 55 | + |
| 56 | +```jsx |
| 57 | +import MyAdmin from "./admin"; |
| 58 | + |
| 59 | +const App = () => <MyAdmin />; |
| 60 | + |
| 61 | +export default App; |
| 62 | +``` |
| 63 | + |
| 64 | +Now, start the server with `yarn dev`, browse to `http://localhost:5173/`, and you should see the working admin: |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +Your app is now up and running, you can start tweaking it. |
0 commit comments