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

225
Views
how to set data in React with axios?

How should one show name and id within the <h1> tag. this project gets its data via axios from an API.

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

const Details = () => {

  const [data, setData] = useState({
    name: "",
    id: "",
  });

  const apiDetails = () => {
    axios
      .get(`https://api.coingecko.com/api/v3/coins/${"ethereum"}`)
      .then((response) => {
        // console.log(response);
        setData({
          name: response.data.name,
          id: response.data.id,
        });
        return setData;
      });
  };

  return (
    <div>
      <h1>{setData.name}</h1>
      <h1>{data.name}</h1>
      <h1>{setData.id}</h1>
      <h1>{data.id}</h1>
      <h1>{setData.name}</h1>
    </div>
  );
};

export default Details;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use a useEffect as so, you can learn more about this hook here

import React, { useState, useEffect } from "react";
import axios from "axios";

const Details = () => {

  const [data, setData] = useState(null);

  const apiDetails = () => {
    axios
      .get(`https://api.coingecko.com/api/v3/coins/${"ethereum"}`)
      .then((response) => {
        // console.log(response);
        //setData({
        //  name: response.data.name,
        //  id: response.data.id,
        // });
        return response;
      });
  };

 useEffect(() => {  
  (async () => {
   const response = await apiDetails(); 
   setData({
      name: response.data.name,
      id: response.data.id,
    });
  })();
 }, [])

  if (data) {
   return (
      <>  
       <h1>{data.name}</h1>
       <h1>{data.id}</h1>
      </>     
   );
  }  

  return null;  
};

export default Details;
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!