Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
Actualice el estado principal del componente secundario y envíe la respuesta al elemento secundario

tengo dos componentes

Componente A -

 const handleLike(id) { ..... handle the logic for the button and setting the state } <ComponentB handleLike={handleLike}/>

Componente B -

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

Este código funciona, pero cambia el estado en el Componente A y el estado no se actualiza en el Componente B

¿Cómo se puede solucionar esto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Componente 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}/>

Componente B -

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

0

Puede usar useState en su componente principal, manejar la devolución de llamada desde el elemento secundario, actualizar el estado y pasar el estado al componente secundario de esta manera:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!