【问题标题】:Use the Datagrid component with custom queries - react-admin使用带有自定义查询的 Datagrid 组件 - react-admin
【发布时间】:2020-12-12 19:49:59
【问题描述】:

在使用带有自定义查询的 Datagrid 组件时收到以下错误。以下代码适用于 react-admin 版本 3.3.1,而不适用于版本 3.8.1

TypeError:无法读取未定义的属性“包含”

浏览器的控制台信息:列表组件必须在 中使用。依赖道具而不是上下文来获取列表数据和回调已被弃用,并且在下一个主要版本的 react-admin 中将不再支持。

参考:https://marmelab.com/react-admin/List.html #提示:您可以将 Datagrid 组件与自定义查询一起使用:

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!

【问题讨论】:

    标签: react-admin


    【解决方案1】:

    从 react-admin 3.7 开始,&lt;Datagrid&gt;&lt;Pagination&gt;ListContext 读取数据,而不是期望通过 props 注入数据。例如,请参阅 https://marmelab.com/react-admin/List.html#the-datagrid-component 上更新的 &lt;Datagrid&gt; 文档。

    如果您将代码包装在 &lt;ListContextProvider&gt; 中,您的代码将可以工作:

    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 >
        )
    }
    

    【讨论】:

    • 感谢您的回答,我完全按照您的说法做了,但遇到了一个错误:TypeError: Cannot read property 'field' of undefined。它是在 Datagrid (Datagrid.js:79) 查找 currentSort.field 的时候。知道如何解决这个问题吗?
    • 我想我解决了我的问题。似乎需要resourceselectedIds 才能工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2020-09-11
    • 1970-01-01
    • 2019-04-16
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多