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

188
Visualizações
React Hooks: Why I have this output?

I'm studying React hooks but i am not able to understand why I got this output in the console. Someone with great heart could explain in detail the "running execution" oh these hooks?

import { useState, useEffect } from "react";
function Homepage() {
  const [state, setState] = useState();
  useEffect(() => {
    console.log("useEffect");
    console.log("useEffect not executed");
    setState("hello")
  }, []);
  if (!state) {
    console.log("state not defined");
    return <div>State undefined</div>;
  }
  return <div>ciao</div>;
}
export default Homepage;

console output: state not defined state not defined useEffect useEffect not executed useEffect useEffect not executed

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

0

Basically it's a combination of React.StrictMode double invoking the function body twice as a way to help you detect unexpected side-effects

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

  • Class component constructor, render, and shouldComponentUpdate methods
  • Class component static getDerivedStateFromProps method
  • Function component bodies <-- this
  • State updater functions (the first argument to setState)
  • Functions passed to useState, useMemo, or useReducer

remounting the component to ensure reusable state

To help surface these issues, React 18 introduces a new development-only check to Strict Mode. This new check will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.

and the useEffect hook being called at the end of the render cycle.

function Homepage() {
  const [state, setState] = useState();

  useEffect(() => {
    console.log("useEffect"); // logs second as expected side-effect
    console.log("useEffect not executed");
    setState("hello");
  }, []);

  if (!state) {
    console.log("state not defined"); // logs first as unintentional side-effect
    return <div>State undefined</div>;
  }

  return <div>ciao</div>;
}

...

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import Homepage from "./Homepage";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <Homepage />
  </StrictMode>
);

Explaining the logs

console output:

state not defined      // <-- initial render
state not defined      // <-- double invocation of function body
useEffect              // <-- effect at end of initial render
useEffect not executed // <-- effect at end of initial render

...unmount/mount

useEffect              // <-- effect at end of render
useEffect not executed // <-- effect at end of render
about 4 years ago · Juan Pablo Isaza Relatório

0

useEffect runs when the page renders and the other functions execute before useEffect so your code runs the if (!state) first then useEffect runs and state sets to "hello"; here is a link to fully understand useEffect hook: useEffect; Good luck;

about 4 years ago · Juan Pablo Isaza Relatório

0

your if statement is running before the useEffect even tho is after it, you need to put both divs inside the return and remove the if

   return (
     {!state ? <div>State undefined</div> : <div>ciao</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