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

97
Visualizações
Update Parent State from Child Component and send back response to child

I have two components,

Component A -

const handleLike(id) {
.....
handle the logic for the button and setting the state
}

<ComponentB handleLike={handleLike}/>

Component B -

return(
    <>
        <button onclick={props.handleLike("123")}/>
    </>
)

<ComponentB handleLike={handleLike}/>

This code works, but it changes the state in the Component A and the state is not updated in the Component B

How can this be solved?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Component A -

const[likeId,setLikeId]= useState(like)
const handleLike(id) {
  .....
   handle the logic for the button and setting the state
  setLikeId(id)
}

<ComponentB likeId={likeId} handleLike={handleLike}/>

Component B -

const { likeId }=props
return(
<>
    <button onclick={props.handleLike("123")}/>
</>
)
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use useState in your parent component, handle callback from child, update the state, and pass the state to child component like this:

const ComponentA= () => {
   const [likes, setLikes] = useState('')

   const handleLikeCallback(id) {
       setLikes(id)
   }

   return <ComponentB handleLikeCallback={handleLikeCallback} likes={likes}/>
}

const ComponentB = ({handleLikeCallback, likes}) => 
    <button onclick={handleLikeCallback("123")}>{likes}</button>
about 4 years ago · Juan Pablo Isaza Relatório
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