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

465
Views
¿Existe una forma más eficiente de obtener datos de una API usando axios? (React.js y nodo.Js)

En esta implementación, el código se ejecuta y logra el propósito de lo que estoy tratando de hacer: usar axios para obtener la API ficticia en https://jsonplaceholder.typicode.com y luego mostrarla en la pantalla. Sin embargo, siento que mi código podría refactorizarse para ser más eficiente. Actualmente, tengo cuatro constantes useState (una para cada propiedad en el objeto), que se configuran/actualizan al obtenerlas. Traté de consolidar las constantes de CurrentFetch CurrentFetch2 CurrentFetch3 CurrentFetch4 en un solo useState({}) , pero React me estaba dando errores por usar objetos aquí. ¿Algunas ideas?

Aquí está mi código:

 import React, { useState } from "react"; import "./App.css"; import axios from "axios"; function App() { const [fetchCount, setFetchCount] = useState(1); const [currentFetch, setCurrentFetch] = useState([]); const [currentFetch2, setCurrentFetch2] = useState([]); const [currentFetch3, setCurrentFetch3] = useState([]); const [currentFetch4, setCurrentFetch4] = useState([]); const HandleFetchApi = async () => { axios .get(`https://jsonplaceholder.typicode.com/todos/${fetchCount}`) .then((response) => { let data = response.data; setCurrentFetch(data.userId); setCurrentFetch2(data.id); setCurrentFetch3(data.title); setCurrentFetch4(data.completed); setFetchCount(fetchCount + 1); }) .catch((error) => { console.log(error); }); }; return ( <div className="App"> <h1>Testing API Fetch from Dummy Api</h1> <button onClick={HandleFetchApi}> Click to fetch record #{fetchCount} </button> <h2>user id: {currentFetch}</h2> <h2>id: {currentFetch2}</h2> <h2>title: {currentFetch3}</h2> <h2>completed: {currentFetch4.toString()}</h2> <br /> <h5>from: https://jsonplaceholder.typicode.com/</h5> </div> ); } export default App;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mejor versión:

 import React, { useState } from "react"; import "./App.css"; import axios from "axios"; function App() { const [fetchCount, setFetchCount] = useState(1); const [currentFetch, setCurrentFetch] = useState({ userId: '', id: '', title: '', completed: '' }); const HandleFetchApi = async () => { axios .get(`https://jsonplaceholder.typicode.com/todos/${fetchCount}`) .then((response) => { const { userId, id, title, completed } = response.data; setCurrentFetch({...currentFetch, ...{userId, id, title, completed}}); setFetchCount(fetchCount + 1); }) .catch((error) => { console.log(error); }); }; return ( <div className="App"> <h1>Testing API Fetch from Dummy Api</h1> <button onClick={HandleFetchApi}> Click to fetch record #{fetchCount} </button> <h2>user id: {currentFetch.userId}</h2> <h2>id: {currentFetch.id}</h2> <h2>title: {currentFetch.title}</h2> <h2>completed: {currentFetch.completed.toString()}</h2> <br /> <h5>from: https://jsonplaceholder.typicode.com/</h5> </div> ); } export default App;

Codificación feliz :)

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!