react PropTypes 与 DefaultProps

PropTypes 与 DefaultProps

 1 import React ,{ Component } from 'react';
 2 import PropTypes from 'prop-types';
 3 class TodoItem extends Component{
 4     constructor(props){
 5         super(props);
 6         this.handleclick=this.handleclick.bind(this);
 7     }
 8     render(){
 9         const { item,test }=this.props;
10         return (
11             <div>
12                 <li 
13                 onClick={this.handleclick}
14                    // /*dangerouslySetInnerHTML={{__html:item,test}}*/
15                 >{item}-{test}</li>
16             </div>
17             )
18     }
19     handleclick(){
20         const { deleteItem,index }=this.props;
21         deleteItem(index);
22     }
23 }
24 TodoItem.propTypes={    // 要求父组件传递给子组件相关的数据参数类型限制
25     test:PropTypes.string.isRequired,
26     item:PropTypes.arrayOf(PropTypes.number,PropTypes.string), //arrayOf指的传递参数要么是数字,要么是字符串
27     deleteItem:PropTypes.func,
28     index:PropTypes.number 
29 }
30 TodoItem.defaultProps={ // 默认传递参数值
31     test:'hello world'
32 }
33 export default TodoItem;

 

转载于:https://www.cnblogs.com/Lolita-web/p/9876529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值