Props & PropTypes

1. Props

"Props are the mechanism React uses to let components communicate with each other.

A parent component can pass it’s child(ren) named prop values, which the child can then

use in its internal logic." Ref[2]

"React components have an internal property ‘props’. This property contains all the props

a component gets from its parent. "  Ref[2]

2. PropTypes

2.1 Introduction

"This is where Reacts propTypes come in. It’s essentially a dictionary where you define what

props your component needs and what type(s) they should be."  Ref[2]

 propTypes: {
//Id can be a number, or a string, but it needs to be defined!
id: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.string
]).isRequired, //Messages should be an object with a title and text property of type string
message: React.PropTypes.shape({
title: React.PropTypes.string,
text: React.PropTypes.string
}).isRequired, //The comments property needs to be an array of objects.
comments: React.PropTypes.arrayOf(React.PropTypes.object), //The date needs to be an instance of type Date.
date: React.PropTypes.instanceOf(Date)
}

2.2 React.PropTypes.shape()

react@15.1.0

A): React.js

 var ReactPropTypes = require('./ReactPropTypes');

 // ......

 var React = {
// ......
PropTypes: ReactPropTypes,
// .......
}; module.exports = React;

B): ReactPropTypes.js

 var ReactPropTypes = {
array: createPrimitiveTypeChecker('array'),
bool: createPrimitiveTypeChecker('boolean'),
func: createPrimitiveTypeChecker('function'),
number: createPrimitiveTypeChecker('number'),
object: createPrimitiveTypeChecker('object'),
string: createPrimitiveTypeChecker('string'), any: createAnyTypeChecker(),
arrayOf: createArrayOfTypeChecker,
element: createElementTypeChecker(),
instanceOf: createInstanceTypeChecker,
node: createNodeChecker(),
objectOf: createObjectOfTypeChecker,
oneOf: createEnumTypeChecker,
oneOfType: createUnionTypeChecker,
shape: createShapeTypeChecker
}; // ......
module.exports = ReactPropTypes;

C): Comment for createShapeTypeChecker function

function createShapeTypeChecker(shapeTypes)

function createChainableTypeChecker(validate)

ReactPropTypes-reading.js

2.3 Use Case of React.PropTypes.shape()

https://github.com/reactjs/react-redux/blob/master/src/utils/storeShape.js

https://github.com/reactjs/react-redux/blob/master/src/components/Provider.js


Reference

1. Prop Validation

https://facebook.github.io/react/docs/reusable-components.html

2. Why React PropTypes are important 

http://wecodetheweb.com/2015/06/02/why-react-proptypes-are-important/

Javascript.Reactjs-5-prop-validation-and-proptypes的更多相关文章

  1. [Javascript] Check both prop exists and value is valid

    Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...

  2. javascript (0, obj.prop)()的用法

    我第一次看到这种奇怪的用法是在babel的源码中, 其实它的原理就是使得在prop这个方法里无法获取this, 从而无法对类中的其他变量或方法做操作. obj.prop() 这是一个方法调用, pro ...

  3. JavaScript 和 React,React用了大量语法糖,让JS编写更方便。

    https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...

  4. JavaScript周报#183

    This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 183Ma ...

  5. 前端工具 - 15个最佳的 JavaScript 表单验证库

    客户端验证在任何项目都是不够的,因为 JavaScript 可以直接忽略,人们可以提交请求到服务器. 然而这并不意味着客户端验证都没必要了,很多时候我们需要在用户提交到服务器之前给予提示.JavaSc ...

  6. javascript——后台传值map类型转换成json对象

    前端需要对后端传过来的值进行解析之后再展示,而后端传过来的值可能是各种类型的,一般情况下要么和后端沟通下让他直接传给我们需要的类型,这个,我一般直接自己转,这次后端传回来一个map类型的对象,我转来转 ...

  7. asp.net mvc 各版本区别

    MVC 6 ASP.NET MVC and Web API has been merged in to one. Dependency injection is inbuilt and part of ...

  8. juery学习总结(一)——juery选择器

    juery在工作中经常使用,遇到不会的问题往往百度一下,事后就忘.使用到现在也感觉不到有什么提高,为了每天进步一点点,从今天起抽时间记录下对juery的学习. 学习之前,首先要了解什么是网页元素,网页 ...

  9. jQuery属性/CSS使用例子

    jQuery属性/CSS 1..attr() 获取匹配的元素集合中的第一个元素的属性的值  或 设置每一个匹配元素的一个或多个属性. 例1:获取元素的属性的值 <p title="段落 ...

随机推荐

  1. 使用spring通知时,代理出错

    动态代理是基于接口的,spring配置是基于类的!!!!!!!!!! 注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的.如本例UserMa ...

  2. MSMQ消息队列 用法

    引言 接下来的三篇文章是讨论有关企业分布式开发的文章,这三篇文章筹划了很长时间,文章的技术并不算新,但是文章中使用到的技术都是经过笔者研究实践后总结的,正所谓站在巨人的肩膀上,笔者并不是巨人,但也希望 ...

  3. cs107

    基本类型:bool,char,short,int,long,float,double 对于char,short,int,long: 多字节类型赋值给少字节类型,对低字节的细节感兴趣,位模式拷贝. 少字 ...

  4. Tesla P4 在深度学习上的性价比辗压目前所有量产的FPGA

    7000的价格, 5.5T FP, 75W不到的功耗,性能接近M40,敢问目前有哪个量产的FPGA能做到?还不算开发和维护的难度...KU115光PCIE+DMA+DDR4 controller+AX ...

  5. TCP应用编程

    TCP是Transmission Control Protocol(传输控制协议)的简称,是TCP/IP体系中面向连接的运输层协议,在网络中提供全双工的和可靠的服务. TCP协议最主要的特点是: 1) ...

  6. mongodb 分组查询

    数据的保存 include_once 'mDB.class.php'; $m=new mDB(); $m->setDB('mydb'); // $m->save('stu',['dept' ...

  7. js中各种事件的兼容性

    1给元素绑定一个事件 ie8attachEvent(on+"事件",event); 高级浏览器 addEventListener("事件",event,fals ...

  8. asp.net 查询好的数据后 排序显示在桌面上

    select top 10 * ,row()_number over ( order by 字段 desc) as rownum  from  表 row()_number;简单的说row_numbe ...

  9. @UniqueConstraint

    jedis UniqueConstraint定义在Table或SecondaryTable元数据里,用来指定建表时需要建唯一约束的列. 元数据属性说明: columnNames:定义一个字符串数组,指 ...

  10. 6.openssl rsautl和openssl pkeyutl

    rsautl是rsa的工具,相当于rsa.dgst的部分功能集合.可用于签名.验证.加密和解密文件.非对称密钥的密钥是随机生成的,因此不需要也无法指定salt参与加密. pkeyutl是非对称加密的通 ...