React PropTypes: range of numbers

前端 未结 4 1294
自闭症患者
自闭症患者 2021-02-14 21:56

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

    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

提交回复
热议问题