【问题标题】:How to fetch data from API using id in reactJS?如何在 reactJS 中使用 id 从 API 获取数据?
【发布时间】:2020-06-04 16:06:45
【问题描述】:

如何将 ID 从一个 API 传递到另一个 API,并获取所需数据?

这是我的代码:

handleClick(e){
    fetch("http://api.com/product_sub_categories?category_id")
    .then(res => res.json())
    .then(
      (result) => {
        this.setState({
          isLoaded: true,
          product_sub_categories: result.product_sub_categories
        });
      },
      (error) => {
        this.setState({
          isLoaded: true,
          error
        });
      }
    )
}

【问题讨论】:

    标签: javascript json reactjs api fetch


    【解决方案1】:

    您可以使用反引号。

    handleClick(e){
      fetch(`${BASE_PATH}/product_sub_categories?category_id=${e.target.value}`)
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            product_sub_categories: result.product_sub_categories
          });
        },
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2021-02-07
      相关资源
      最近更新 更多