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

109
Vistas
How do we change the state of element in a react component on button click in another component?

I want to add the active class on an element in componentOne on the button click in componentTwo. I am using useState for it but I don't know how to pass the state into another component. The componentTwo is the child of the componentOne.

I want the active class componentOne as long as the button is not clicked again even when the browser loads again

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

just create a state in the parent component and pass it down as a prop

cosnt [active , setActive] =useState(false)

<ParentComponent>
    <componentOne active={active} , setActive={setActive} />
     <componentTwo active={active} , setActive={setActive} />
</ParentCompnent>

Component One :

  const ComponentOne = ({active}) => {
  return(
        <div className={`${active && 'activeClass'}`}></div>
   )  
 }


Component Two :

  const  ComponentTwo = ({setActive}) => {
  return(
        <button onClick={()=> setActive(true)}>
   )  
 }
about 4 years ago · Juan Pablo Isaza Denunciar
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