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

156
Visualizações
Closure with callback

Ok, here's a closure issue (i think) that I'm dealing with. Can't wrap my head around it. I have a component and I'm trying to get the updated value of shouldDropAbove into my callback onStepItemDrop:

export const StepItem = () => {
  const [shouldDropAbove, setShouldDropAbove] = React.useState<boolean>()

  React.useEffect(() => {
    console.log(shouldDropAbove) //outputs updated value correctly
  }, [shouldDropAbove])

  console.log(shouldDropAbove) //also is outputting the updated value correctly

  const onStepItemDrop = (draggingStep) => {
    console.log(shouldDropAbove) //outputting undefined ?????
  }

  return (
    <Droppable
      onDrop={onStepItemDrop}
      setShouldDropAbove={setShouldDropAbove} //Droppable updates setShouldDropAbove correctly
    >
      {jsx stuff}
    </Droppable>
  )
}
export const Droppable = ({onDrop, setShouldDropAbove}) => {
  //updating setShouldDropAbove
}

What is going on here and why can't I get the correct value in the callback?

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