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

209
Vistas
How to use useState in addEventListener?

If I rightClick the red DivElement, I want to change my state

As the state has been changed, so I thought I could see console.log

But It does not work

Here is my code

 import { useState, useRef, useEffect } from "react";

function Main() {
  const [testState, setTestState] = useState(0);
  const testRef = useRef(null);

  function clickHandler(event) {
    setTestState(testState + 1);
  }

  useEffect(() => {
    if (!testRef) return;

    testRef.current.addEventListener('contextmenu', (event) => { clickHandler(event) });
  }, [testRef]);

  useEffect(() => {
    console.log('testState: ', testState);
  }, [testState]);

  return (
    <div
      ref={testRef}
      style={{width: '100%', display: 'block', height: '32px', backgroundColor: 'red'}}
    />
  );
}

export default Main;
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

the problem comes when you are referencing your state, keep in mind that setState is executed asynchronously (think about set state as a request to rerender the component).

by changing your setTestState(testState + 1); setTestState((oldVal) => oldVal + 1); your example will work. take a look to this stackblitz example

regards,

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should use a cleanup function and preventDefault on clickHandler.

import { useState, useRef, useEffect, useCallback } from "react";

function Main() {
  const [testState, setTestState] = useState(0);
  const testRef = useRef(null);

  const clickHandler = useCallback((event) => {
    event.preventDefault();
    setTestState((test) => test + 1);
  }, []);

  useEffect(() => {
    const refElement = testRef.current;
    refElement.addEventListener("contextmenu", clickHandler);
    return () => refElement.removeEventListener("contextmenu", clickHandler); //cleanup function
  }, [clickHandler]);

  useEffect(() => {
    console.log("testState: ", testState);
  }, [testState]);

  return (
    <div
      ref={testRef}
      style={{
        width: "100%",
        display: "block",
        height: "32px",
        backgroundColor: "red"
      }}
    />
  );
}

export default Main;

Working Codesandbox

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a working code snippet:

function Main() {
  const [testState, setTestState] = useState(0);
  const testRef = useRef(null);

  useEffect(() => {
    function clickHandler(event) {
      setTestState((old) => old + 1);
    }
    if (!testRef) return;
    const ref = testRef.current;
    ref.addEventListener("contextmenu", clickHandler);
    return () => ref.removeEventListener("contextmenu", clickHandler);
  }, [testRef, testState]);

  useEffect(() => {
    console.log("testState: ", testState);
  }, [testState]);

  return (
    <div
      ref={testRef}
      style={{
        width: "100%",
        display: "block",
        height: "32px",
        backgroundColor: "red",
      }}
    />
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
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