【发布时间】:2020-07-11 22:39:34
【问题描述】:
我对这个简单的示例感到非常沮丧,因为要么我没有得到任何东西,要么它没有按照我期望的方式以及文档中所说的方式工作。
所以我试图扩展 CloneButton 组件以实际检索 Datagrid 每一行上的记录并将其传递给 create 组件:
import React from 'react';
import PropTypes from 'prop-types';
import { CloneButton, useGetOne } from 'react-admin';
import CircularProgress from '@material-ui/core/CircularProgress';
const CloneQueryButton = ({
label = "Duplicate",
basePath,
record,
...props
}) => {
const { data, loading, error } = useGetOne(basePath, record.id);
if (loading) { return <CircularProgress />; }
if (error) { return <p>ERROR {error.message}</p>; }
if (data) {
console.log("inside", data);
data.name = `Copy of ${data.name}`;
}
console.log("outside", data);
return <CloneButton
basePath={basePath}
record={data}
label={label}
{...props} />
};
我在控制台上得到的只是outside null
此外,当按下按钮时,它会重定向到带有空 record 属性的创建页面。
我可以看到网络请求,它们都返回有效的 json。我不明白为什么在收到响应时组件显然不会重新呈现。如果有人能看到我错过了什么,我将非常感激!
【问题讨论】:
-
您是否使用
<Resource标签作为您要获取的类型? -
是的,我已将该类型包含在
Resource标记中。这就是我访问放置按钮的 List 组件的方式。如果没有Resource标签,请求就不会成功。
标签: reactjs react-hooks react-admin