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

190
Vistas
I'm using useState to change the state of an element based on if a checkbox if checked or not, but the state is not updating - couldnt find reason
import { useCallback, useEffect, useState, useRef } from 'react';

const [changeEl, setChangeEl] = useState(false);
let chosenInput = useRef();
let result = useRef();
let allCheckboxes = [];

 const checkHandler = useCallback(
    async (e) => {
      const allInputs = document.querySelectorAll('input');
      for (const input of allInputs) {
        if (input.type === 'checkbox') {
          allCheckboxes.push(input);
        }
      }
      chosenInput.current = e.target;
      if (chosenInput.current.name === 'notChecked') {
        chosenInput.current.name = 'checked';
      } else {
        chosenInput.current.name = 'notChecked';
      }

      for (const [i, value] of result.current.entries()) {
        if (
          chosenInput.current.name === 'checked'
        ) {
          allCheckboxes[i].disabled = true;
          console.log(allCheckboxes[i].disabled) //TRUE
          chosenInput.current.disabled = false;
          setChangeEl(allCheckboxes[i].disabled); 
          console.log(ChangeEl) //THIS IS STILL FALSE BUT IT IS SUPPOSED TO CHANGE

        }
        if (chosenInput.current.name !== 'checked') {
          allCheckboxes[i].disabled = false;
          setChangeEl(allCheckboxes[i].disabled); 
          console.log(changeEl) //THIS WORKS
        }
      }
    },
    [changeEl, allCheckboxes]
  );

the changeEl is false even when I'm setting it to true. But it is false when I'm setting it to false. I think it has something to do with the useState not updating it immediately but I'm not sure it's updating it at all. If I'm using useContext to update it it works but I don't want to do that because it is unnecessary. I couldn't find an answer for this so I would really appreciate the help.

about 4 years ago · Santiago Gelvez
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