无法从useEditController获取数据

问题描述

我使用React Admin,并且我想创建一个自定义编辑页面,所以我使用useEditController从dataProvider.getone()获取数据。但是,当我想重组数据时,由于未定义,它无法这样做。我认为这是因为useEditController是一个异步调用,我们必须在破坏数据之前等待它。

这是MyEdit.js中的代码

const {
basePath,defaultTitle,loaded,loading,record,redirect,resource,save,saving,version,} = useEditController(props)
const { videoKey } = record

运行此命令时,它说“无法解构'record'的属性'videoKey',因为它是未定义的。”

解决方法

实际上,useEditController甚至在API响应record的调用之前返回。这意味着recordloaded为真之前是不确定的。

因此,我建议您仅在record值为loaded的情况下尝试对true进行解构。像这样:

if (!loaded) return null; // or a loading component of your own
const { videoKey } = record;
...