Home > Back-end >  TypeError: this.state.data.map in reactjs
TypeError: this.state.data.map in reactjs

Time:09-30

class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      isLoaded: false,
    };
  }

  componentDidMount() {
    fetch("https://reqres.in/api/users?page=2")
      .then((res) => res.json())
      .then((json) => {
        this.setState({
          isLoaded: true,
          data: json,
 
        });
      });
  }

  render() {

var { isLoaded, data }= this.state;

if(!isLoaded){
  return<div>Is isLoaded</div>
}

else{

    return (
      <div>
        <ul>
          {() =>
            this.state.data.map((data, index) => (
              <li key={index}>Email: {data.email}</li>
            ))
          }
          ;
        </ul>
        
      </div>
    );
  }
}
}

export default Home;

Hii All , I know this question is asked many times but I cant figure it out I'am getting the error. I have checked for all the questions similar to this but haven't found specific solution if I use another link i.e, "https://jsonplaceholder.typicode.com/users" this one the code works fine .

CodePudding user response:

The returned data from https://reqres.in/api/users?page=2 is not an array, but an object with a data property containing what you are looking for (an array). The result of the request is :

{"page":1,"per_page":6,"total":12,"total_pages":2,"data":[{"id":1,"email":"[email protected]","first_name":"George","last_name":"Bluth","avatar":"https://reqres.in/img/faces/1-image.jpg"},{"id":2,"email":"[email protected]","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},{"id":3,"email":"[email protected]","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},{"id":4,"email":"[email protected]","first_name":"Eve","last_name":"Holt","avatar":"https://reqres.in/img/faces/4-image.jpg"},{"id":5,"email":"[email protected]","first_name":"Charles","last_name":"Morris","avatar":"https://reqres.in/img/faces/5-image.jpg"},{"id":6,"email":"[email protected]","first_name":"Tracey","last_name":"Ramos","avatar":"https://reqres.in/img/faces/6-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}

So you cannot use enter image description here

Working Example: StackBlitz

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      isLoaded: false,
    };
  }

  componentDidMount() {
    fetch('https://reqres.in/api/users?page=2')
      .then((res) => res.json())
      .then((json) => {
        console.log(json.data);
        this.setState({
          isLoaded: true,
          data: json.data,
          email: null,
        });
      });
  }

  render() {
    var { isLoaded, data } = this.state;

    if (!isLoaded) {
      return <div>Is isLoaded</div>;
    } else {
      return (
        <div>
          <div className="contents home">
            <img
              src="https://trucard.io/india/wp-content/uploads/2021/08/2021-June-TruCard-Logo.png
"
              width={50}
              alt="img"
              className="trucard-img"
            />
          </div>
          <div className="button">
            <button className="button-button">Load list</button>
          </div>
          <ul>
            {this.state.data?.map((data, index) => (
              <li key={index}>Email: {data.email}</li>
            ))}
            ;
          </ul>
        </div>
      );
    }
  }
}

export default App;

  • Related