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

97
Vistas
How to pass a state value to parent component after the state is updated?
 const [horizontal, setHorizontal] = useState(10);
const [vertical, setVertical] = useState(10);
const [blur, setBlur] = useState(10);
const [spread, setSpread] = useState(10);
const [color, setColor] = useState('#000000');
const [checked, setChecked] = useState(false);

const onChangeHandler = (setFunction,event) => {
    if (setFunction === setChecked) {
        setFunction((prevVal) => !prevVal);
    }
    else {
        setFunction(event.target.value);
    }
    props.valuesInputHandler({
        horizontal,
        vertical,
        blur,
        spread,
        color,
        checked
    });
};


<input type="checkbox" checked={checked} onChange={onChangeHandler.bind(this,setChecked)} />

The onChangeHandler function runs when there is any change in the input field. Now suppose I clicked on a checkbox, the onChangeHandler will run. Since I passed setChecked function as the parameter, the setChecked function will turn te false value to true. After that this value will be passed to the parent component through props function. But the problem is when I click on the checkbox, the checked state gets updated, but before getting updated, the value of checked is already passes. So if becomes true, the parent component will still have the previous false value. And when I reclick checkbox, it becomes false, but the parent component gets the previous true value this time. It is causing one step delay. How can I pass the updated state value??

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