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

173
Views
How to use state in great grand parent in react

What is the easiest way to set state from 4 components below the first component?

  • First {name}
  • Second
  • Third
  • Fourth {setName}

In fourth component I want to set name and then use it in the first component to render the name like {name}

const First = () => {
  const [name, setName] = useState();
<div>
  <div>{name}
  <Second />
</div>
}

const Second = () => {
 <div>
  <Third />
 </div>
}

const Third = () => {
 <div>
  <Fourth />
 </div>
}

const Fourth = () => {
 <div>
  <div onClick={()=>setName(something.name)}>{something.name). 
  </div>
 </div>
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In the fourth component you can have a handleNameChange(newName) prop to lift the state up.

const FourthComponent = (
  name: string,
  handleNameChanged: (newName: string) => void
) = {
  const onNameChanged = (newName: string) => handleNameChanged(newName)
}

Same in third and second components to lift up in the same way.

Finally, in the first one you will have something like :

const FirstComponent = () => {
  const [name, setName] = useState<string>('')

  <SecondComponent
    name={name}
    handleNameChange={(name) => setNAme(name)}
  />
}
about 4 years ago · Juan Pablo Isaza Report

0

In a case you want to pass a variable to a parent, you just pass it via a prop function:

const Parent = () => {
    const [var, setVar] = useState();

    useEffect(() => {
        console.log(`${var} in parent`);

    return (
        <Child passVarToParent={setVar} />
    );
}

const Child = (props) => {
    return (
        <input /* ... */ onChange={(e) => props.passVarToParent(e.target.value)} />
    );
}

However, in case of parent-child-chaining, I'd go with some state management like Redux, MobX or React Context.

about 4 years ago · Juan Pablo Isaza Report

0

You can do this by passing setName as a prop from the First component to the Fourth

const First = () => {
  const [name, setName] = useState();
  return (
    <div>
      {name}
      <Second setFirstName={(name) => setName(name)} />
    </div>
  );
}

const Second = ({ setFirstName }) => {
  return (
   <div>
     <Third setFirstName={setFirstName}/>
   </div>)
}

const Third = ({ setFirstName }) => {
 return (
  <div>
    <Fourth setFirstName={setFirstName}/>
  </div>
 )
}

const Fourth = ({ setFirstName }) => {
 return (
   <div>
     <div onClick={() => setFirstName('Test Name')}>Press to set Name</div>
   </div>
 )
}
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!