Use el componente Datagrid con consultas personalizadas - react-admin

Aug 24 2020

Reciba los siguientes errores al usar el componente Datagrid con consultas personalizadas. El siguiente código funciona con react-admin ver 3.3.1, mientras que no funciona con ver 3.8.1

TypeError: no se puede leer la propiedad 'incluye' de indefinido

Información de la consola del navegador: los componentes de la lista deben usarse dentro de un <ListContext.Provider>. Depender de accesorios en lugar de contexto para obtener datos de la lista y devoluciones de llamadas está obsoleto y no será compatible con la próxima versión principal de react-admin.

Referirse:https://marmelab.com/react-admin/List.html#Consejo: Puede usar el componente Datagrid con consultas personalizadas:

import keyBy from 'lodash/keyBy'
import { useQuery, Datagrid, TextField, Pagination, Loading } from 'react-admin'

const CustomList = () => {
    const [page, setPage] = useState(1);
    const perPage = 50;
    const { data, total, loading, error } = useQuery({
        type: 'GET_LIST',
        resource: 'posts',
        payload: {
            pagination: { page, perPage },
            sort: { field: 'id', order: 'ASC' },
            filter: {},
        }
    });

    if (loading) {
        return <Loading />
    }
    if (error) {
        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"
            >
                <TextField source="id" />
                <TextField source="name" />
            </Datagrid>
            <Pagination
                page={page}
                perPage={perPage}
                setPage={setPage}
                total={total}
            />
        </>
    )
} ```

Please help!

Respuestas

4 FrançoisZaninotto Sep 08 2020 at 00:43

Desde react-admin 3.7, <Datagrid>y <Pagination>lea datos de un ListContextarchivo , en lugar de esperar que los accesorios inyecten los datos. Consulte, por ejemplo, los <Datagrid>documentos actualizados enhttps://marmelab.com/react-admin/List.html#the-datagrid-component.

Su código funcionará si lo envuelve en un <ListContextProvider>:

import React, { useState } from 'react';
import keyBy from 'lodash/keyBy'
import { useQuery, Datagrid, TextField, Pagination, Loading, ListContextProvider } from 'react-admin'

export const CustomList = () => {
    const [page, setPage] = useState(1);
    const perPage = 50;
    const { data, total, loading, error } = useQuery({
        type: 'GET_LIST',
        resource: 'posts',
        payload: {
            pagination: { page, perPage },
            sort: { field: 'id', order: 'ASC' },
            filter: {},
        }
    });

    if (loading) {
        return <Loading />
    }
    if (error) {
        return <p>ERROR: {error}</p>
    }
    return (
        <ListContextProvider value={{
                data: keyBy(data, 'id'),
                ids: data.map(({ id }) => id),
                total,
                page,
                perPage,
                setPage,
                currentSort: { field: 'id', order: 'ASC' },
                basePath: "/posts",
                resource: 'posts',
                selectedIds: []
        }}>
            <Datagrid rowClick="edit">
                <TextField source="id" />
                <TextField source="name" />
            </Datagrid>
            <Pagination />
        </ListContextProvider >
    )
}