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

326
Vistas
React PropTypes: how to specify PropType for children of component to accept two types of subcomponents?

I'm currently building a large component that should only take two components as children (in this case, a TopBar and a NavBar). So far, I've tried specifying the propType for the children as this:

AppHeader.propTypes = {
  /**
   * AppHeader should ONLY accept TopBar and NavBar components as children.
   */
  children: PropTypes.arrayOf([
    PropTypes.instanceOf(TopBar),
    PropTypes.instanceOf(NavBar),
  ]),
};

However, the PropTypes aren't working for me. I'm facing this problem with a similar component that can take a random number of two types of subcomponents.

How should I go about creating this PropTypes for the children where you specify the type of components that it can take. (This is not an enum since an enum is 'either or'/ oneOf).

Thanks so much in advance!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I didn't test this but it seems that PropTypes.arrayOf doesn't accept an Array. Instead try providing the PropTypes.oneOfType object. Then pass your array into that.


AppHeader.propTypes = {
  children: PropTypes.arrayOf(
    PropTypes.oneOfType([
      PropTypes.instanceOf(TopBar),
      PropTypes.instanceOf(NavBar),
    ])
  ),
};

references: https://reactjs.org/docs/typechecking-with-proptypes.html#proptypes https://blog.logrocket.com/validating-react-component-props-with-prop-types-ef14b29963fc/

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