用React过滤API结果

时间:2019-04-28 08:01:55

标签: javascript reactjs api axios


我通过以下代码片段来使用API​​:

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得到了这样的结果:

enter image description here

我的问题是,如何过滤API结果,然后使用数据中的结果。?

1 个答案:

答案 0 :(得分: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
    });
}