Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
How to render this array in array

I want to show this advice from this API I have fetched all the data but when I try to show it, it throws the error, and also I tried to map the data but nothing works it shows errors

import React, { useState, useEffect } from 'react';

export default function App() {
  const [advices, setAdvices] = useState([]);
  console.log(advices); // you should have the fetch data here

  async function fetchData() {
    try {
      const response = await fetch('https://api.adviceslip.com/advice');
      const data = await response.json();
      setAdvices(data.slip);
    } catch (error) {
      console.error(error);
    }
  }

  useEffect(() => {
    fetchData();
  }, []);

  return <></>;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

import React, {
  useState,
  useEffect
} from 'react';

export default function App() {
  const [advices, setAdvices] = useState([]);
  //advices will always be null here as initiated with empty array
  console.log(advices); // you should have the fetch data here 

  async function fetchData() {
    try {
      const response = await fetch("http://jsonplaceholder.typicode.com/Posts");
      const data = await response.json();
      setAdvices(data);
    } catch (error) {
      console.error(error);
    }
  }

  useEffect(() => {
    fetchData();
  }, []);

  useEffect(() => {
    console.log(advices);
  }, [advices]);

  return ( < > Test < />);
  }

about 4 years ago · Juan Pablo Isaza Relatório

0

the initial value of advice should be an object and there was not another serious problem codesand box link

    import { useEffect, useState } from "react";
import "./styles.css";

export default function App() {
  const [advices, setAdvices] = useState([]);
  console.log(advices); // you should have the fetch data here

  async function fetchData() {
    try {
      const response = await fetch("https://api.adviceslip.com/advice");
      const data = await response.json();
      setAdvices(data.slip);
    } catch (error) {
      console.error(error);
    }
  }

  useEffect(() => {
    fetchData();
  }, []);
  return (
    <div className="App">
      <h1>{advices.advice}</h1>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda