如何使用React-Table库获取Json

我引用的示例如下: https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/sub-components

我想获取一个json文件并将后者的元素传递到表中,而不是像前面的示例那样生成它们。 提取操作的说明对我很清楚,但我不明白如何将它们集成到“ makeData”文件中。

这是我的“ makeData”代码:

import ReactDOM from 'react-dom';

const range = len => {
  const arr = []
  for (let i = 0; i < len; i++) {
    arr.push(i)
  }
  return arr
}

const newPerson = () => {
  fetch('http://localhost:5000/api/azienda')
  .then(res => res.json())


 //   .then((rows) => {
  //     ReactDOM.render(this.makeData(rows),document.getElementById('root'));
   //   });
}

export default function makeData(...lens) {
  const makeDataLevel = (depth = 0) => {
    const len = lens[depth]
    return range(len).map(d => {
      return {
        ...newPerson(),subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined,}
    })
  }

  return makeDataLevel()
}

对于任何建议,我都会感谢您

DJJ980620 回答:如何使用React-Table库获取Json

在构造函数中创建一个数组,该数组将存储数据,从服务器链接获取数据,然后将其放入表中

  constructor(props,context) {
    super(props,context);
    this.state = { items: []};
  }


  getList = () => {
    fetch("http://localhost:5000/api/azienda")
      .then(res => res.json())
      .then(items => this.setState({ items }))
      .catch(err => console.log(err));

  };

  componentDidMount() {
      this.getList();
  }
本文链接:https://www.f2er.com/3082293.html

大家都在问