gpt4 book ai didi

react-admin - 将 Datagrid 组件与自定义查询一起使用 - react-admin

转载 作者:行者123 更新时间:2023-12-02 16:30:35 24 4
gpt4 key购买 nike

将 Datagrid 组件与自定义查询一起使用时,收到以下错误。下面的代码适用于 react-admin ver 3.3.1,而不适用于 ver 3.8.1

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

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

引用:https://marmelab.com/react-admin/List.html#Tip:您可以将 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 3.7 开始,<Datagrid><Pagination>ListContext 读取数据,而不是期望数据被 Prop 注入(inject)。例如,请参阅更新的 <Datagrid>文档位于 https://marmelab.com/react-admin/List.html#the-datagrid-component .

如果您将代码包装在 <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 >
)
}

关于react-admin - 将 Datagrid 组件与自定义查询一起使用 - react-admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63556508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com