Advanced guide--typechecking with Proptypes

Source: Internet
Author: User

The react has a built-in type detection mechanism for component props run type detection, which requires the Proptypes attribute to be developed:

class Greeting extends React.component {  render () {    return  (      this .props.name}    );   = {  name:React.PropTypes.string};

React.proptypes outputs a series of validators to ensure that the data you receive is legitimate.

First, React.proptypes

The following is an example of a different validator recorded;

Mycomponent.proptypes = {  //you can declare that a prop is a specific JS raw data  //These are optional by defaultOptionalArray:React.PropTypes.array, OptionalBool:React.PropTypes.bool, OptionalFunc:React.PropTypes.func, op TionalNumber:React.PropTypes.number, OptionalObject:React.PropTypes.object, optionalstring: React.PropTypes.string, OptionalSymbol:React.PropTypes.symbol,//anything that can be rendered: numbers, strings, elements or an array  //(or fragment) containing these types.OptionalNode:React.PropTypes.node,//A React element.OptionalElement:React.PropTypes.element,//declares that a prop is an instance of a class and uses the JS instanceof operatoroptionalMessage:React.PropTypes.instanceOf (Message),//you can use enum to ensure that your prop is limited to specific valuesOptionalEnum:React.PropTypes.oneOf ([' News ', ' Photos ')]),  //an object that can be one of many typesOptionalUnion:React.PropTypes.oneOfType ([React.PropTypes.string, React.PropTypes.number, React.proptypes. Instanceof (Message)]),//contains an array of a specific typeoptionalArrayOf:React.PropTypes.arrayOf (React.PropTypes.number),//object property is a specific type of objectoptionalObjectOf:React.PropTypes.objectOf (React.PropTypes.number),//an object of a specified formOptionalObjectWithShape:React.PropTypes.shape ({color:React.PropTypes.string, fontSize:React.PropTypes.numb ER}),//you can use any of these validators to link ' isrequired ' to ensure that prop must have a valuerequiredFunc:React.PropTypes.func.isRequired,//a value of any typerequiredAny:React.PropTypes.any.isRequired,//You can also customize the validator, and if validation fails, you must return an Error object  //do not use Console.warn or throw, these are useless in oneoftype.Customprop:function(props, propname, componentname) {if(!/matchme/. Test (Props[propname])) {      return NewError (' Invalid prop ' + propname + ' supplied to ' + ' + ' + componentname + '. Validation failed. '      ); }  },  //You can also provide a validator for arrayof and Objectof  //if validation fails, it should also return an Error object  //in array or object, the validator is called for each key.  //the first two arguments of the validator are the array or object itself and the current key valueCustomArrayProp:React.PropTypes.arrayOf (function(PropValue, Key, ComponentName, location, propfullname) {if(!/matchme/. Test (Propvalue[key])) {      return NewError (' Invalid prop ' + Propfullname + ' supplied to ' + ' + ' + componentname + '. Validation failed. '      ); }  })};

Advanced guide--typechecking with Proptypes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.