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

306
Views
Los datos de API no se procesan: reacciona

Estoy tratando de generar datos de la API pero, por alguna razón, no puedo hacerlo.

 const [ data, setData ] = useState([]) const fetchData = () => { return ( fetch('https://mocki.io/v1/b9c63035-97c5-40a0-b45c-2abdf5261bdf') .then(response => response.json()) .then(data => setData(data)) .catch(error => { console.log('Error fetching and parsing data', error) }) ) } useEffect(() => { fetchData() }, []);

console.log('API DATA', data) Recibo la respuesta, pero no estoy seguro de por qué no se puede realizar el bucle. Supongo que es porque los datos están en formato JSON.

 return ( { data.length > 0 && data.map((item, index) => { <div key={index}>{item.title}</div> }) } )

ingrese la descripción de la imagen aquí

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

0

No está devolviendo nada después de mapear a través de la matriz, actualice su declaración de devolución como:

 return ( <> {data.length > 0 && data.map((item, index) => { return <div key={index}>{item.title}</div>; })} </> );

o

 return ( <> {data.length > 0 && data.map((item, index) => <div key={index}>{item.title}</div>)} </> );

Además, las expresiones JSX deben tener solo un elemento principal. Asegúrese de envolver la devolución en un fragmento , es decir, <>

about 4 years ago · Juan Pablo Isaza Report

0

Prefiero axios y useRef para API

 const [myData, setMyData, myDataRef] = useStateRef([]); const getData = () => { axios .get("https://mocki.io/v1/b9c63035-97c5-40a0-b45c-2abdf5261bdf") .then((res) => { let data = res.data; console.log(data); setMyData(data); }); }; useEffect(() => { getData(); }, []); <div> {myDataRef.current.length > 0 && myDataRef.current.map((item, index) => { return <div key={index}>{item.title}</div>; })} </div>

codigosandbox

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!