最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Creating custom PropTypes that extend default PropTypes in react - Stack Overflow

matteradmin6PV0评论

I'm trying to create a custom PropType that checks if an array has numerical values and has a length of 2 (and there's some constraint on the ordering of the numbers).

Obviously I can do Array.PropType.arrayOf(Array.PropType.number) for the first two constraints.

I'd like to reuse this in my custom PropType (instead of rolling out my own numerical and array check).

How do I do that?

I'm trying to create a custom PropType that checks if an array has numerical values and has a length of 2 (and there's some constraint on the ordering of the numbers).

Obviously I can do Array.PropType.arrayOf(Array.PropType.number) for the first two constraints.

I'd like to reuse this in my custom PropType (instead of rolling out my own numerical and array check).

How do I do that?

Share Improve this question asked Feb 2, 2016 at 19:12 praks5432praks5432 7,81232 gold badges93 silver badges158 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

React.PropTypes.arrayOf( React.PropTypes.number ) just returns a function, so you can instead provide your own function to perform the validation.

Your function will be passed three parameters: props, propName, ponentName

see the second to last example shown in React.PropTypes from the docs.

So a function that should satisfy your constraints would be:

function isTwoElementArrayOfNumbers( props, propName, ponentName ){
  var _array = props[ propName ];

  if( _array && _array.constructor === Array && _array.length === 2 ){

    if( !_array.every(
      function isNumber( element ){
        return typeof element === 'number';
      })
    ){

      return new Error('elements must be numbers!');
    }
  }
  else{
    return new Error('2 element array of numbers not provided!');
  }
}

...in your react element
propTypes:{
  numArray: isTwoElementArrayOfNumbers
},

I don't know about extending them, I'm not sure that's possible.

But you could store the above as an exported constant and use it to pose properties of other propTypes (but I think you probably already know this):

export const CustomProp = Array.PropType.arrayOf(Array.PropType.number)

export const OtherCustomProp = contains({
 foo: React.PropTypes.string,
 bars: CustomProp
});
Post a comment

comment list (0)

  1. No comments so far