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

263
Views
Necesito ayuda (Api llama en React Js Hooks) ¿Por qué no funciona esto?

Necesito ayuda (Api llama en React Js Hooks) ¿Por qué no funciona esto? Necesito llamar a los valores de esa API

 import React, { useEffect, useState } from 'react'; function Customers() { const [customers, setCustomers] = useState(null); useEffect(() => { fetch('https://reactstarter-app.herokuapp.com/api/customers') **API CALLS** .then(res => res.json()) .then(customers => setCustomers(customers)) }, []) return ( <div> <h2>Customers</h2> <ul> {customers.map((customer) => { return <li key={customer.id}>{customer.firstName} {customer.lastName}</li> })} </ul> </div> ); } export default Customers;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Tal vez no sea una solución, pero no puedo pegar el código para comentar, así que tengo que publicar una respuesta:

 function Customers() { // this is where you declare the "customers" const const [customers, setCustomers] = useState(null); useEffect(() => { fetch('https://reactstarter-app.herokuapp.com/api/customers') **API CALLS** .then(res => res.json()) // this is where you should change the "customers" to "data" // because of the variable name conflict .then(data => setCustomers(data)) }, [])
about 4 years ago · Juan Pablo Isaza Report

0

Parece que está tratando de mapear a través de un estado nulo y probablemente obtenga un error, use la representación condicional para evitar el error y representar a los clientes después de la solicitud de API:

 import React, { useEffect, useState } from 'react'; function Customers() { const [customers, setCustomers] = useState(null); useEffect(() => { fetch('https://reactstarter-app.herokuapp.com/api/customers') **API CALLS** .then(res => res.json()) .then(customers => setCustomers(customers)) }, []) return ( <div> <h2>Customers</h2> {!customers ? <h2>Loading customers...</h2> : <ul> {customers.map((customer) => { return <li key={customer.id}>{customer.firstName} {customer.lastName}</li> })} </ul>} </div> ); } export default Customers;

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!