react PropTypes 与 DefaultProps
PropTypes 与 DefaultProps
import React ,{ Component } from 'react';
import PropTypes from 'prop-types';
class TodoItem extends Component{
constructor(props){
super(props);
this.handleclick=this.handleclick.bind(this);
}
render(){
const { item,test }=this.props;
return (
<div>
<li
onClick={this.handleclick}
// /*dangerouslySetInnerHTML={{__html:item,test}}*/
>{item}-{test}</li>
</div>
)
}
handleclick(){
const { deleteItem,index }=this.props;
deleteItem(index);
}
}
TodoItem.propTypes={ // 要求父组件传递给子组件相关的数据参数类型限制
test:PropTypes.string.isRequired,
item:PropTypes.arrayOf(PropTypes.number,PropTypes.string), //arrayOf指的传递参数要么是数字,要么是字符串
deleteItem:PropTypes.func,
index:PropTypes.number
}
TodoItem.defaultProps={ // 默认传递参数值
test:'hello world'
}
export default TodoItem;
react PropTypes 与 DefaultProps的更多相关文章
- react中PropTypes与DefaultProps的应用
每个组件都有自己的props参数,这参数是从父组件接收的一些属性,那么如何对参数的类型作校验.如何定义参数的默认值.这里涉及到两个基础的概念,叫做proptypes 和 defaultprops.子组 ...
- react+propTypes
React.createClass({ propTypes: { // 可以声明 prop 为指定的 JS 基本数据类型,默认情况,这些数据是可选的 optionalArray: React.Prop ...
- 关于React.PropTypes的废除,以及新版本下的react的验证方式
React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...
- propTypes和 defaultProps
propTypes和 defaultProps propTypes: 可以 用来做类型的校验 限制类型 isRequired 必须要求传递 要使用必须先引入: import PropTypes fro ...
- react 中的PropTypes与DefaultProps
每个组件都有自己的props参数,这参数是从父组件接收的一些属性.那我们应该如何对参数的类型做校验,如何定义参数的默认值呢? 1.使用PropTypes校验父组件传过来的参数是否合法 import P ...
- TypeScript 3.0下react默认属性DefaultProps解决方案
ts和react的默认属性的四种解决方案 Non-null assertion operator(非空断言语句) Component type casting(组件类型重置) High order f ...
- [React] Remove React PropTypes by using Flow Annotations (in CRA)
Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...
- react创建组件的几种方式及其区别
react创建组件有如下几种方式 ①.函数式定义的无状态组件 ②.es5原生方式React.createClass定义的组件 ③.es6形式的extends React.Component定义的组 ...
- [Full-stack] 快速上手开发 - React
故事背景 [1] 博客笔记结合<React快速上手开发>再次系统地.全面地走一遍. [2] React JS Tutorials:包含了JS --> React --> Red ...
随机推荐
- SQLServer数据库中开启CDC导致事务日志空间被占满的原因
SQLServer数据库中开启CDC导致事务日志空间被占满的原因 转载 2017-04-01 投稿:mrr 我要评论 这篇文章主要介绍了SQLServer数据库中开启CDC导致事务日志空间 ...
- mysql权限表
http://blog.csdn.net/zhangliangzi/article/details/51882710
- Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法
Win 2008 R2安装SQL Server 2008“性能计数器注册表配置单元一致性”失败的解决办法(2011-02-23 19:37:32) 转载▼ 今天在惠普服务器上安装数据库2008时, ...
- jquery datatable 多行(单行)选择(select),行获取/行删除
jquery datatable 多行(单行)选择(select),行获取/行删除 代码展示 // 示例数据源 var dataSet = [ ['Tasman','Internet Explorer ...
- Struts2框架06 ValueStack
原文地址:点击前往 1 什么是ValueStack 称为值栈,Struts提供的共享数据的数据结构 2 为什么要使用ValueStack 从控制器向浏览器传递数据 存储与请求相关的对象信息(sessi ...
- unity3d MonoDevelop引用外部自定义dll文件报错:are you missing an assembly reference?
在unity3d 编辑器 MonoDevelop 中引用外部自定义dll文件报错:are you missing an assembly reference? 因为unity还停留在.NET Fram ...
- review backpropagation
The goal of backpropagation is to compute the partial derivatives ∂C/∂w and ∂C/∂b of the cost functi ...
- Fast Scatter-Gather I/O
Some applications may need to read or write data to multiple buffers, which are separated in memory. ...
- C#/Python/MATLAB操作PostgreSQL数据库
PostgreSQL数据库是一个功能非常强大的开源数据库,支持多种SQL特性,非常好用.此外由于结合PostGIS可以实现空间数据库功能,故非常适合GIS领域的使用.本文旨在介绍C#.Python.M ...
- 在robotframework里面,怎么在已有的字典中加一个键值对呢