如何将值从数据提供程序传递到创建/编辑页面



我正在创建一个 react-admin 应用程序,其中将图像上传到服务器后,响应将包含我必须在该模块的创建/编辑页面中更新image_id。

我尝试更新记录,但它没有得到更新。

class ImageUpload extends Component {
handleClick = (params) => {
const { push, record, showNotification } = this.props;
const updatedRecord = { ...record, is_approved: true };
var imgElement = document.getElementById('series_image');
console.log(record)
if(imgElement.files && imgElement.files[0]){
var filereader = new FileReader();
filereader.readAsArrayBuffer(imgElement.files[0]);
filereader.onload = function(e) {
fetch(`http://localhost:53111/b/admin/img/v1/upload`, { 
method: 'POST', 
body : filereader.result,
headers: {
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => record.image_id = data.imageId)
.catch((e) => {
showNotification('Error: comment not approved', 'warning')
});
}

}
}
render() {
return <input type='file' id="series_image" onChange={this.handleClick} />;
}   
}

image_id正在数据提供程序中更新

编号: 2798image_id:"269">是巡回赛:1 is_confirmed: 1 is_history_present: 0 is_video_present: 1

但是在更新记录时,image_id恢复为默认值

编号: 2798image_id: 0是旅游: 1 is_confirmed: 1 is_history_present: 0 is_video_present: 1

请指导我如何在收到服务器的响应后更新记录。

显示数据时,react-admin 从 Redux 状态获取数据:admin.resource."my_resource_name".data

使用下载方法,您无法正确修改 Redux 状态。

文件下载示例: https://marmelab.com/react-admin/DataProviders.html#decorating-your-data-provider-example-of-file-upload

查询接口: https://marmelab.com/react-admin/Actions.html

最新更新