Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Simple example with Preact is firing twice on useState

I have a tiny Preact component where I want to toggle the visibility of a div, but it is not working as expected. The following recreates the issue:

import { h, render } from 'preact';
import { useState } from 'preact/hooks';

function App() {
  const [isOpen, setIsOpen] = useState(true);

  function toggle() {
    console.log('toggle');
    setIsOpen(!isOpen);
  }

  console.log(isOpen);

  if (isOpen) {
    return (
      <div>
        <div onClick={toggle}>Close</div>
      </div>
    );
  } else {
    return <div onClick={toggle}>Open</div>;
  }
}

render(<App />, document.body);

But it doesn't work like I would expect. The console output is the following:

true
toggle
false
toggle
true

First two lines are from initial rendering. All of the rest happens when I click Close. I would expect Close to change to Open, and the console output to be the following when I click Close:

toggle
false

I use this to build:

esbuild ./tmp.tsx --bundle --watch --minify --outdir=./ --jsx-factory=h --jsx-fragment=Fragment

and the html is

<html>
  <body>
    <script async src="tmp.js"></script>
  </body>
</html>

What is going on here?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda