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

327
Visualizações
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 Respostas
Responde à pergunta

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 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