Ten minutes to learn the type detection library supporting react - the use of prop-types

js sometimes does not report an error when the type of the variable is defined as number or string, so prop-types is specially used to detect react. The previous version put it into the react architecture, and now it is moved out as an independent library. Same as redux;

1. First you need to install a third-party package called prop-types by npm install prop-types --save in the terminal

2. Then type-check the variables in the props of one of your components by writing the following:

     组件名.propTypes = {
         属性1:属性1的变量类型,
         属性2:属性2的变量类型
        //...
     }

3. It does not detect null undefined ;

4. Detection type:

       Son.propTypes = {
          optionalArray: PropTypes.array,//检测数组类型
          optionalBool: PropTypes.bool,//检测布尔类型
          optionalFunc: PropTypes.func,//检测函数(Function类型)
          optionalNumber: PropTypes.number,//检测数字
          optionalObject: PropTypes.object,//检测对象
          optionalString: PropTypes.string,//检测字符串
          optionalSymbol: PropTypes.symbol,//ES6新增的symbol类型
      }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324907897&siteId=291194637