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

99
Visualizações
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 à 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