React-Admin JSON Server Data Provider
ra-data-json-server is a data provider specifically designed to integrate `react-admin` applications with REST APIs that follow the conventions of JSON Server, including services like JSONPlaceholder. It translates `react-admin` data requests (e.g., `getList`, `getOne`, `create`) into standard HTTP requests compatible with a JSON Server backend. The package is currently at version 5.14.5 and is actively maintained, with frequent patch releases addressing bug fixes and minor improvements, usually on a weekly or bi-weekly basis, following the `react-admin` release cycle. A key differentiator is its strict adherence to JSON Server's REST dialect, simplifying setup for prototyping or light backend needs, and requiring specific HTTP headers like `X-Total-Count` for proper pagination.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'total')
cause The `X-Total-Count` header is missing or inaccessible in the `getList` or `getManyReference` response, preventing `react-admin` from determining the total number of records.fixVerify that your backend sends the `X-Total-Count` header. If using a different domain for your API, ensure `Access-Control-Expose-Headers: X-Total-Count` is configured in your backend's CORS policy. -
CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause Your `react-admin` application is trying to access an API on a different domain without proper CORS headers configured on the server-side.fixConfigure your backend server to include appropriate `Access-Control-Allow-Origin` headers (e.g., `Access-Control-Allow-Origin: *` for development, or specific origins in production) and `Access-Control-Expose-Headers` for `X-Total-Count`. -
React-admin displays 'No results found' even though the API returns data.
cause This often happens when `react-admin` cannot parse the `X-Total-Count` header, leading it to believe there are zero records, or if the API response format does not exactly match expectations (e.g., `id` field name).fixInspect network requests to confirm the `X-Total-Count` header is present and readable. Also, ensure your API's primary key field is named `id` as expected by default, or provide a custom `id` mapper if it's named differently.
Warnings
- breaking The `ra-data-json-server` provider requires the backend API to return an `X-Total-Count` header for `getList` and `getManyReference` calls. Without it, pagination and total record counts will not function correctly in `react-admin`.
- gotcha When developing with `ra-data-json-server` against a different origin API, browser security policies (CORS) might block access to custom headers like `X-Total-Count`. This will lead to pagination issues in `react-admin`.
- gotcha The `updateMany` and `deleteMany` operations are implemented as multiple individual `PUT` or `DELETE` requests, not a single batch request, matching JSON Server's default behavior. This can lead to performance overhead for large batch operations.
- gotcha Recent fixes indicate potential issues with `id` values of `0` in `useCreate` and stale nested field values in `SimpleFormIterator`. While these are fixed in 5.14.5, older versions might exhibit incorrect cache updates or form state behavior.
Install
-
npm install ra-data-json-server -
yarn add ra-data-json-server -
pnpm add ra-data-json-server
Imports
- jsonServerProvider
import { jsonServerProvider } from 'ra-data-json-server';import jsonServerProvider from 'ra-data-json-server';
- jsonServerProvider
const jsonServerProvider = require('ra-data-json-server');import jsonServerProvider from 'ra-data-json-server';
- httpClient configuration
import { fetchUtils } from 'react-admin'; const httpClient = (url, options = {}) => { /* ... */ return fetchUtils.fetchJson(url, options); }; const dataProvider = jsonServerProvider('url', httpClient);
Quickstart
import * as React from 'react';
import { Admin, Resource } from 'react-admin';
import jsonServerProvider from 'ra-data-json-server';
// For demonstration purposes, we'll use a simple list component.
// In a real app, you'd define your own PostList.
const PostList = () => <div>Posts List (render your actual list here)</div>;
const App = () => (
<Admin dataProvider={jsonServerProvider('https://jsonplaceholder.typicode.com')}>
<Resource name="posts" list={PostList} />
<Resource name="users" list={PostList} />
</Admin>
);
export default App;