【问题标题】:react-admin: When to first get data pass by a List to a DataGridreact-admin:何时首先将数据通过 List 传递给 DataGrid
【发布时间】:2019-10-26 01:54:23
【问题描述】:

我有一个替换 DataGrid 组件的自定义组件,它使用状态,所以我想用一个代表记录的布尔项数组来启动状态道具。 我的问题是在哪里启动我的组件的 state prop,因为我需要首先访问来自后端的记录。

根据文档,List 组件“...将数据传递给迭代器视图 - 通常是 Datagrid...”并查看它在 componentDidMount 和 componentWillRecibeProps 方法中执行此操作的源代码。 但到目前为止我无法解决。

简而言之, DataGrid的数据什么时候加载进去?

有什么建议吗? 谢谢。

【问题讨论】:

    标签: react-admin


    【解决方案1】:

    您可以在渲染这些方式之前修改来自后端的数据

    1. 可能最适合修改的地方是convertHTTPResponsedataProvider.js

    dataProvider.js:

    const convertHTTPResponse = async (response, type, resource, params) => {
      let json;
      switch (type) {
        case GET_LIST:
        case GET_MANY:
        case GET_MANY_REFERENCE:
          json = await response.json();
          const total = response.headers.get('x-total-count');
          return {
            data: json.map(d => /* modify data here */),
            total: parseInt(total, 10)
          };
    <..>
    
    export default (type, resource, params) => {
    <..>
      return fetch(url, options)
        .then(res => convertHTTPResponse(res, type, resource, params));
    };
    
    1. 修改自定义组件中的数据
    const ListComponent = (({ classes, ...props }) => (
      <List {...props}>
        <YourCustomComponent />
      </List>
    ));
    
    const YourCustomComponent = ({ ids, data, basePath }) => {
      // modify data here
      return // 
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2023-04-05
      • 2020-10-23
      • 2016-01-01
      • 2017-04-28
      相关资源
      最近更新 更多