【问题标题】:how to use map function with (axios/classhooks) to read from API如何使用带有 (axios/classhooks) 的 map 函数从 API 中读取
【发布时间】:2021-02-14 10:12:48
【问题描述】:
import React, { Component } from "react";
import axios from "axios";
class Verifry extends Component {
  constructor(props) {
    super(props);
    this.state = {
      s: "0",
      user: [],
    };
  }

/* 具有标题作为 res.data 中的属性*/ 异步组件DidMount() { 等待 axios .get(http://10.0.0.106:8080/kuwait_elections/api/about_us) .then((res) => { 常人 = res.data; this.setState({用户:persons.data.title,s:“4”}); 控制台.log(this.state.user); }); } 组件DidUpdate() { // this.state.user.map((u) => { // 返回

  • ; // }); } 使成为() { 返回 (
      {this.state.user.map((t) => { 返回
    • {t.title}
    • ; })}
    ); } }
    export default Verifry;
    

    【问题讨论】:

    • 你能格式化你的代码sn-p吗?

    标签: reactjs axios


    【解决方案1】:

    您的退货似乎不正确。应该是这样的。

    {
      this.state.user.map(({title}) => { 
        return { title };
      })
    }
    

    注意:请正确格式化您的代码,以便于理解。

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2021-09-05
      • 2019-02-07
      • 2020-09-10
      • 2018-07-05
      • 1970-01-01
      • 2021-04-22
      • 2020-12-17
      • 2013-04-12
      相关资源
      最近更新 更多