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="段落 ...
随机推荐
- 如何在tpl模版的div块中加ztree
ld-ztree.tpl <div class="ld-ztree-container"> <div class="ld-ztree-header te ...
- go语言环境搭建+sublime text3(windows环境下)
感觉有点坑,整了一下午~搞定 go语言环境搭建+sublime text3(windows环境下) 1.安装sublime text3 2.安装go语言程序包 3.测试go语言是否安装成功 键 ...
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Android 6.0权限适配
targetSdkVersion 23以上,必须适配新的权限模式 安卓6.0及之后,权限分为三类 1.不涉及隐私的正常权限,如innernet2.危险权限 3.特殊权限 system_alert_w ...
- 移动端自动化环境搭建-stuptools和pip的安装
安装stuptools和pip A.安装依赖 setuptools 和 pip 并非必须安装的两个包,但安装之后,后续再安装 Python 的库将变得非常简单. B.安装过程
- net命令
net命令可以完成非常多的任务.通过键入 net /? 可以查看net命令的详细列表. 在所有的Windows机器上,net命令使用统一的命令集合,这对于网络管理员来说是非常方便的. 使用net命令可 ...
- winform开发之UI系列
1.如何构造一个漂亮的主窗体 主要讲述如何对一个新建窗体的美化过程,涉及到经常需要用到的几个属性我会着重强调它的用法,并不断更新它,因为楼主也正在探索中.... 步骤如下: vs新建一个winform ...
- 画图解释SQL联合语句
画图解释SQL联合语句 http://blog.jobbole.com/40443/ 我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.S ...
- Linux内核中断学习
1.内核中断概述 (1)在OS环境下编写中断处理函数与之前在裸机中编写中断处理函数的方式是不一样的,在Linux内核中提供了一套用来管理硬件中断资源的软件体系架构. (2)在操作系统中,中断号与gpi ...
- asp.net LINQ防止SQL注入式攻击
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...