Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda