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

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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