Bassam Foaud
Bassam Foaud

Reputation: 1

how to use map function with (axios/classhooks) to read from API

import React, { Component } from "react";
import axios from "axios";
class Verifry extends Component {
  constructor(props) {
    super(props);
    this.state = {
      s: "0",
      user: [],
    };
  }

/* has title as attribute within the res.data*/ async componentDidMount() { await axios .get(http://10.0.0.106:8080/kuwait_elections/api/about_us) .then((res) => { const persons = res.data; this.setState({ user: persons.data.title, s: "4" }); console.log(this.state.user); }); } componentDidUpdate() { // this.state.user.map((u) => { // return

  • u
  • ; // }); } render() { return ( ); } }

    export default Verifry;
    

    Upvotes: 0

    Views: 376

    Answers (1)

    Paveloosha
    Paveloosha

    Reputation: 623

    Seems your return is not correct. It should be like this.

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

    Note: Please format your code properly to make it easier to understand.

    Upvotes: 0

    Related Questions