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