Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
How to set JSON array to state in React?

I have this bellow App.js code:

import React, { Component } from 'react';
import axios from 'axios'


class Axios extends Component {

    constructor() {
        super();
        this.state = {
            item : '',
        }
    }

    componentDidMount() {
        axios.get('https://restcountries.com/v3.1/capital/lima')
        .then( response => {
            const data = response.data.map(( data )=>{
                this.setState({
                    item : data
                });
            });
        })
        .catch( error => {
            alert( error );
        });
    }

    prepare() {
        console.log( this.state.item );
    }

    render() {
        return (
            <div>{this.prepare()}</div>
        )
    }
}

export default Axios;

My goal is to get the common name from this API: https://restcountries.com/v3.1/capital/lima

Now on componentDidMount() method, I need to set the API return data to the item state so that I can loop through using the prepare method.

But I don't have any idea how to set the API return array JSON data to the item state?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You just have to do assing response.data to item as:

Live Demo

Codesandbox Demo

componentDidMount() {
  axios
    .get("https://restcountries.com/v3.1/capital/lima")
    .then((response) => {
        this.setState({
          item: response.data
        })
      })
    .catch((error) => {
      alert(error);
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

Update your state.item to a blank array.

constructor() {
    super();
    this.state = {
        item : [],
    }
}

In componentDidMount(), update the code to accept response :

 componentDidMount() {
    axios.get('https://restcountries.com/v3.1/capital/lima')
    .then( response => {
        this.setState({
                item : response.data
            });
    })
    .catch( error => {
        alert( error );
    });
}

In render(), you can use map on state.item and can loop on it.

render() {
    return (
        <div>{this.state.item.map(data,index)=>(
              //some UI mapping to each `data` in `item` array
        )}</div>
    )
}
about 4 years ago · Juan Pablo Isaza Report

0

You should check this out.

import React, { Component } from "react";
import axios from "axios";

class Axios extends Component {
  constructor() {
    super();
    this.state = {
      item: []
    };
  }

  componentDidMount() {
    axios
      .get("https://restcountries.com/v3.1/capital/lima")
      .then((response) => {
        this.setState({
          item: response && response.data
        });
      })
      .catch((error) => {
        alert(error);
      });
  }
  
  prepare() {
    return <div>{console.log("Item", this.state.item)}</div>;
  }

  render() {
    return <div>{this.prepare()}</div>;
  }
}

export default Axios;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!