Javascript.Reactjs-5-prop-validation-and-proptypes
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)
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的更多相关文章
- [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 ...
- javascript (0, obj.prop)()的用法
我第一次看到这种奇怪的用法是在babel的源码中, 其实它的原理就是使得在prop这个方法里无法获取this, 从而无法对类中的其他变量或方法做操作. obj.prop() 这是一个方法调用, pro ...
- JavaScript 和 React,React用了大量语法糖,让JS编写更方便。
https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...
- JavaScript周报#183
This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 183Ma ...
- 前端工具 - 15个最佳的 JavaScript 表单验证库
客户端验证在任何项目都是不够的,因为 JavaScript 可以直接忽略,人们可以提交请求到服务器. 然而这并不意味着客户端验证都没必要了,很多时候我们需要在用户提交到服务器之前给予提示.JavaSc ...
- javascript——后台传值map类型转换成json对象
前端需要对后端传过来的值进行解析之后再展示,而后端传过来的值可能是各种类型的,一般情况下要么和后端沟通下让他直接传给我们需要的类型,这个,我一般直接自己转,这次后端传回来一个map类型的对象,我转来转 ...
- asp.net mvc 各版本区别
MVC 6 ASP.NET MVC and Web API has been merged in to one. Dependency injection is inbuilt and part of ...
- juery学习总结(一)——juery选择器
juery在工作中经常使用,遇到不会的问题往往百度一下,事后就忘.使用到现在也感觉不到有什么提高,为了每天进步一点点,从今天起抽时间记录下对juery的学习. 学习之前,首先要了解什么是网页元素,网页 ...
- jQuery属性/CSS使用例子
jQuery属性/CSS 1..attr() 获取匹配的元素集合中的第一个元素的属性的值 或 设置每一个匹配元素的一个或多个属性. 例1:获取元素的属性的值 <p title="段落 ...
随机推荐
- rsync 通过 ssh 上传文件
rsync -avzP /www/ -e ssh root@192.168.1.200:/www/ 批处理上传: #!/bin/bash for (( ; ; )) do rsync -avzP /w ...
- "System Protection" is disabled in Win10 default settings
We could find some important clue in Restore Point because "System Protection" of volume C ...
- .NET (二)委托第二讲:内置委托Func
在上一章节中,我们自己声明了一个委托: public delegate bool Cal(int num); 接受int参数,返回bool类型,目的是过滤集合中的 奇数 或者 偶数. .NET 为我们 ...
- MapReduce工作原理讲解
第一部分:MapReduce工作原理 MapReduce 角色•Client :作业提交发起者.•JobTracker: 初始化作业,分配作业,与TaskTracker通信,协调整个作业.•TaskT ...
- Unity已经学会的
1.以MonoBehaviour为脚本的开发模式. 2.MonoBehaviour的大多数API. 3.动画系统大多数了解. 4.UI了解一些,能写UI. 5.Editor了解一些,能写Editor. ...
- 关于C#的委托(delegate)的自我理解
首先描述一个事情,一个老师饿了,他要去买东西填饱肚子,然后他发现他的学生“小李”在玩,没学习,于是就委托“小李”去帮他买吃的. 根据这件事我们来分析: 首先得有个老师(老师饿了是他的方法,老师买东西也 ...
- <读书笔记>软件调试之道 :问题的核心-修复后的反思
声明:本文档的内容主要来源于书籍<软件调试修炼之道>作者Paul Butcher,属于读书笔记.欢迎转载! ---------------------------------------- ...
- lua学习笔记(一)
lua是一种嵌入式的语言,首先安装. lua安装依赖于readline库,下载readline tar -zxvf readline-6.2.tar.gz cd readline-6.2 ./conf ...
- 40、dom以xml结尾的文件
1.student.xml文件 <?xml version="1.0" encoding="utf-8" ?> <!-- 1.书写根元素(因为 ...
- ActiveX: 如何用.inf和.ocx文件生成cab文件
ActiveX: 如何用.inf和.ocx文件生成cab文件