React PropTypes: range of numbers

前端 未结 4 2158
深忆病人
深忆病人 2021-02-14 22:31

Is there a better way to validate if a number is inside a range?

Avoiding to write

PropTypes.oneOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 
         


        
4条回答
  •  甜味超标
    2021-02-14 22:40

    You can use custom Prop validator.

    completed: function(props, propName, componentName) {
        if (props[propName]>=1 &&  props[propName]<=10) {
          return new Error(
            'Invalid prop `' + propName + '` supplied to' +
            ' `' + componentName + '`. Validation failed.'
          );
        }
      }
    

    Please refer the documentation for further details.

    https://reactjs.org/docs/typechecking-with-proptypes.html

提交回复
热议问题