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

203
Vistas
How to add a prop to a child component already receiving props via the spread operator?

I'm not sure if this is a TypeScript issue, or if it's more of a React props behavior question, or if it's simply me messing up ES6 destructuring.

I have a ParentComponent whose props are type ParentProps, and a ChildComponent which also has the same type. The ChildComponent consumes the same props, except for ONE prop which I now need to redefine (or if that's not possible, add a new one).

For this example let's say ParentProps is just buttonName: string and doSomething: (text: string) => void

I thought I could add to an object destructuring by spreading one object, then adding property: value after a comma, ie: {...props, additionalProp: doSomething} But that doesn't seem to work: Left side of comma operator is unused and has no side effects. Can anyone chime in on what my issue is here and what I'm doing wrong?

const ParentComponent: FC<ParentProps> = memo(function ParentComponent({
  ...props
}) {
  function doSomething(text) {
    console.log("did the thing", text)
  }

  return <ChildComponent {...props, doSomething: doSomething} />   // <----here's the problem
})

export function ChildComponent(props: ParentProps) {
  return (
    <button onClick={props.doSomething(props.buttonName)}>{props.buttonName}</button>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

...or if it's simply me messing up ES6 destructuring.

   return <ChildComponent {...props} doSomething={doSomething} /> //  do this

You have to destructure an object (or array). You were confusing the JSX-property brackets with the brackets used to declare an object.

The example above will also overwrite a potentially existing property props.doSomething. React-props will overwrite previously defined props. However, it would be better if you were explicit about this change instead of implicitly overwriting it.

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