Skip to content

Commit

Permalink
[doc] Fix rendering a Datagrid outside a Resource instructions
Browse files Browse the repository at this point in the history
refs #5020
  • Loading branch information
fzaninotto committed Oct 8, 2020
1 parent f5804db commit 7d7b4c3
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions docs/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -2159,12 +2159,12 @@ const PostList = props => (
export default withStyles(styles)(PostList);
```

**Tip**: You can use the `Datagrid` component with [custom queries](./Actions.md#usequery-hook):
**Tip**: You can use the `Datagrid` component with [custom queries](./Actions.md#usequery-hook), provided you pass the result to a `<ListContextProvider>`:

{% raw %}
```jsx
import keyBy from 'lodash/keyBy'
import { useQuery, Datagrid, TextField, Pagination, Loading } from 'react-admin'
import { useQuery, Datagrid, TextField, Pagination, Loading, ListContextProvider } from 'react-admin'

const CustomList = () => {
const [page, setPage] = useState(1);
Expand All @@ -2186,24 +2186,28 @@ const CustomList = () => {
return <p>ERROR: {error}</p>
}
return (
<>
<Datagrid
data={keyBy(data, 'id')}
ids={data.map(({ id }) => id)}
currentSort={{ field: 'id', order: 'ASC' }}
basePath="/posts" // required only if you set use "rowClick"
rowClick="edit"
>
<ListContextProvider
value={{
resource: 'posts',
basePath: 'posts',
data: keyBy(data, 'id'),
ids: data.map(({ id }) => id),
currentSort: { field: 'id', order: 'ASC' },
selectedIds: [],
}}
>
<Datagrid rowClick="edit">
<TextField source="id" />
<TextField source="name" />
<TextField source="title" />
</Datagrid>
<Pagination
page={page}
perPage={perPage}
setPage={setPage}
total={total}
/>
</>
</ListContextProvider>
);
)
}
```
Expand Down

0 comments on commit 7d7b4c3

Please sign in to comment.