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

224
Visualizações
Accessing state in a parent component from a child component

I'm currently accessing the state of a parent component in a child component through the use of callbacks, but I'm not quite sure how it works:

export default function parentComponent({ type }) {

    const
         [inEditMode, setInEditMode] = useState(false)

return (
        <>
        
            <div className={containerCssClasses}>
                    <childComponent onCancel={() => { setInEditMode(false); }} type={type} />

here, the parent component is calling the child component and passing an anonymous function to the component for it to call back.

In the child component:

function childComponent({onCancel}) {
 return (
    <div>
        <Button onClick={() => onCancel()} variant="link" >
    </div?
 );
}

I am a bit confused how this is able to work properly if the child component doesn't have access to setInEditMode(). I understand that when passing a function to another function (in this case, a component), it is passed by reference. But does that necessarily mean all of the variables within that function are passed by reference as well? I am new to react (and javascript), but when I looked this concept up I couldn't find a good explanation.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your implementation is the right way to go. More than callbacks these functions you pass down to child components are call props and I invite you to read the documentation about them.

https://reactjs.org/docs/components-and-props.html

You can also find very detailed articles about it:

https://itnext.io/what-is-props-and-how-to-use-it-in-react-da307f500da0

Whatever you'll pass from the parent to the child can be used in the child component as if it was called in the parent one.

In your case the child is indeed gonna trigger setInEditMode(false)

As you're not passing any variable to it you can actually call it as such:

<Button onClick={onCancel} variant="link" >
about 4 years ago · Juan Pablo Isaza Relatório

0

The parent component's state you pass to child component as props is not updated as parent's state is changed. so, you need a callback function to access parent's current state at any given time. Also, if you have dependent state value in parent and child component or rather multiple components, you can move it to reducer if you're using redux for state management.

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