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

102
Views
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 answers
Answer question

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 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!