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

153
Visualizações
Change state of parent component in child component

I want to change a value in the parent component by changing the state in the child component which is passed by props.

// This is the parent component
const [showEdit, setShowEdit] = useState(false);
<EditApplicationSettings
  appSettings={appSettings}
  changeState={(showEdit) => setShowEdit(showEdit)}
/>

// This is the child component
export default function EditApplicationSettings({ appSettings, props }) {
  return (
    <button
      className="button-red"
      onClick={() => props.changeState(false)}
    >
      Cancel
    </button>
  );
}

When I click on the button, that should change the state in parent, but instead, I get an error.

TypeError: Cannot read properties of undefined (reading 'changeState')

Where did I do wrong in passing the props?

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

0

In React terms props tends to refer to the entire property object:

EditApplicationSettings(props)

But since you're destructuring the properties from the object you need to reference the changeState property explicitly:

EditApplicationSettings({ appSettings, changeState })

and

onClick={() => changeState(false)}
about 4 years ago · Juan Pablo Isaza Relatório

0

To solve this error

TypeError: Cannot read properties of undefined (reading 'changeState')

then this line

export default function EditApplicationSettings({ appSettings, props }) {

should be

export default function EditApplicationSettings({ appSettings, ...props }) {
                                                               ^^^

You could read more at MDN doc for destructuring assignment

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