Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!