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

197
Views
How to enforce Array shape and size with Prop-Types?

I'm trying to create a proptype that can either be a string or an array with only 1 string and 1 object shape e.g. [10, { message: "hello world", type: "success" }]

Currently, I have:

static propTypes = {
    myProp: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.arrayOf(
            PropTypes.oneOfType([PropTypes.string, myObjectShape])
        ),
    ]),
 }

This will accept a string or an array that can have any amount of strings or object shapes. Is there a way to limit the array to only 1 string and 1 object shape?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a custom prop type. Something along the lines of...

myProp: function(props, propName, componentName) {
    if (!props[propName] || !props[propName].length !== 2) {
      return new Error(`${propName} must be an array with length 2`);
    }

    if (typeof props[propName][0] !== 'string') {
      return new Error(`The first element in ${propName} must be a string`);
    }

    // check for the shape of the object at index 1...
}

More about it here.

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!