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

231
Visualizações
ReactJS how to update page after fetching data

I'm new to ReactJS and I'm now trying to do an interactive comments section (taken from frontendmentor.io), but the App component just doesn't show what it's supposed to show

This is my App component:

function App() {
    const [data, setData] = useState([]);

    useEffect(() => {
        const getComm = async () => {
            await fetchData();
        };

        getComm();
    }, []);
    console.log(data);

    const fetchData = async () => {
        const res = await fetch("db.json").then(async function (response) {
            const comm = await response.json();
            setData(comm);
            return comm;
        });
    };
    return (
        <Fragment>
            {data.length > 0 ? <Comments data={data} /> : "No Comments to Show"}
        </Fragment>
    );
}

export default App;

The console.log(data) logs two times:

  • the first time it's an empty Array;
  • the second time it's the Array with my datas inside.

As it follows: console.log(data)

If I force the App to print the Comments it just says that cannot map through an undefined variable

the error when displaying "Comments" component

This is my Comments component:

function Comments({ data }) {
    return (
        <div>
            {data.map((c) => (
                <Comment key={c.id} />
            ))}
        </div>
    );
}

export default Comments;

I'm wondering why the page still displays No Comments to Show even if the log is correct

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

0

The initial state of your data is an array.

After you fetch your data from the response you get an object. Changing state types is not a good practice. You should keep your data state as an array or as an object.

Considering you will keep it as an array, you need use an array inside of setData. Ex.

comm && Array.isArray(comm.comments) && setData(comm.comments);

As for your Comments component you should consider expecting an array not an object.

Ex.

function Comments(data) {
    return (
        <div>
            {data.map((c) => (
                <Comment key={c.id} />
            ))}
        </div>
    );
}

export default Comments;
about 4 years ago · Juan Pablo Isaza Relatório

0

@Cristian-Irimiea Have right about response get from fetch. Response is an a object and can't be iterate. You need to store in state the comments from response

But you have multiple errors:

  1. Take a look how use async function. Your function fetchData looks bad.
// Your function
const fetchData = async () => {
        const res = await fetch("db.json").then(async function (response) {
            const comm = await response.json();
            setData(comm);
            return comm;
        });
    };

// How can refactor
// fetchData function have responsibility to only fetch data and return a json
const fetchData = async () => {
  const response = await fetch("./db.json");
  const body = await response.json();

  return body;
};

  1. You are updating state inside fetch function but a good solution is update state then promise resolve:
useEffect(() => {
  // here we use .then to get promise response and update state
  fetchData().then((response) => setData(response.comments));
}, []);
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