【问题标题】:Filter API result with React使用 React 过滤 API 结果
【发布时间】:2019-09-17 04:26:36
【问题描述】:

我使用 API 与这段代码的 sn-p 反应:

constructor(props) {
  super(props);
  this.state = {
    items: null
  };
}

componentDidMount() {
  return client
    .get('/api/v1/product/list')
    .then(response => {
      this.setState({items: response});
      return response
    })
    .catch(err => {
      throw err
    });
}

然后我从 API 得到这样的结果:

我的问题是,如何过滤 API 结果,然后使用 data..中的 results

【问题讨论】:

  • 我的问题被 2 票否决,顺便说一句,我的问题有什么问题..?如果我的问题有问题,我会对其进行编辑和改进。

标签: javascript reactjs api axios


【解决方案1】:

试试这样,

constructor(props) {
  super(props);
  this.state = {
    items: null
  };
}

componentDidMount() {
  return client
    .get('/api/v1/product/list')
    .then(response => {
      //extract data from response 
       let {data} = response;
      this.setState({items:data.results});
      return response
    })
    .catch(err => {
      throw err
    });
}

【讨论】:

  • 谢谢你,你真的知道我想要的 :)
猜你喜欢
  • 1970-01-01
  • 2014-10-23
  • 2021-07-22
  • 2019-12-25
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多