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

504
Vistas
¿Cómo asignar la escritura correcta a React.cloneElement al dar propiedades a los niños?

Estoy usando React y Typescript. Tengo un componente de reacción que actúa como contenedor y deseo copiar sus propiedades a sus hijos. Estoy siguiendo la guía de React para usar el elemento de clonación: https://facebook.github.io/react/blog/2015/03/03/react-v0.13-rc2.html#react.cloneelement . Pero cuando uso React.cloneElement , aparece el siguiente error de Typescript:

 Argument of type 'ReactChild' is not assignable to parameter of type 'ReactElement<any>'.at line 27 col 39 Type 'string' is not assignable to type 'ReactElement<any>'.

¿Cómo puedo asignar la tipificación correcta a react.cloneElement?

Aquí hay un ejemplo que replica el error anterior:

 import * as React from 'react'; interface AnimationProperties { width: number; height: number; } /** * the svg html element which serves as a wrapper for the entire animation */ export class Animation extends React.Component<AnimationProperties, undefined>{ /** * render all children with properties from parent * * @return {React.ReactNode} react children */ renderChildren(): React.ReactNode { return React.Children.map(this.props.children, (child) => { return React.cloneElement(child, { // <-- line that is causing error width: this.props.width, height: this.props.height }); }); } /** * render method for react component */ render() { return React.createElement('svg', { width: this.props.width, height: this.props.height }, this.renderChildren()); } }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

El problema es que la definición de ReactChild es esta:

 type ReactText = string | number; type ReactChild = ReactElement<any> | ReactText;

Si está seguro de que child es siempre un ReactElement , cámbielo:

 return React.cloneElement(child as React.ReactElement<any>, { width: this.props.width, height: this.props.height });

De lo contrario, use la protección de tipo isValidElement :

 if (React.isValidElement(child)) { return React.cloneElement(child, { width: this.props.width, height: this.props.height }); }

(No lo he usado antes, pero según el archivo de definición está ahí)

over 4 years ago · Santiago Trujillo Denunciar

0

Esto lo resolvió para mí:

 React.Children.map<ReactNode, ReactNode>(children, child => { if(React.isValidElement(child)) { return React.cloneElement(child, props ) } }

solución bastante simple

over 4 years ago · Santiago Trujillo 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