当前位置:网站首页>1-8 basic use of props
1-8 basic use of props
2022-07-28 23:59:00 【Endless cakes】
props The basic use of
React I have uploaded the dependent package to the following address :https://download.csdn.net/download/weixin_39162041/86264167
<script type="text/babel">
// Create components
class Person extends React.Component{
render(){
const {name,age,sex} = this.props
return (
<ul>
<li> full name :{name}</li>
<li> Gender :{age}</li>
<li> Age :{sex}</li>
</ul>
)
}
}
// Type tag properties . The limits of necessity .
Person.defaultProps={ // If the value is not transmitted or is empty , Default values can be set
sex:' manly woman ',
age:18
}
// Type tag properties 、 The limits of necessity
// Premise : Need to introduce <script type="text/javascript" src="./js/prop-types.js"></script>
Person.propTypes = {// Be sure to pay attention to the case of keywords
name:PropTypes.string.isRequired, //name Must be of string type , Must pass
sex:PropTypes.string,//sex Must be of string type
age:PropTypes.number,//age Must be of numeric type
speak:PropTypes.func // When setting the restriction property of function type , by func ( Particular attention ) .
}
ReactDOM.render(<Person name='tom' age='18' sex=' male ' speak='speak'/>,document.getElementById('test'))
// If there are many data , You can use the expansion operator , combination babel and React When the grammar component of is worth passing , It can be used to expand an object , as follows :
ReactDOM.render(<Person {...data} />,document.getElementById('test'))
function speak(){
}
</script>
//props Shorthand way
<script type="text/babel">
// Create components
class Person extends React.Component{
// Type tag properties . The limits of necessity .
static defaultProps={
sex:' manly woman ',
age:18
}
static propTypes = {
name:PropTypes.string.isRequired,
sex:PropTypes.string,
}
state = {}
render(){
const {name,age,sex} = this.props
return (
<ul>
<li> full name :{name}</li>
<li> Gender :{age}</li>
<li> Age :{sex}</li>
</ul>
)
}
}
ReactDOM.render(<Person name='tom' age='18' sex=' male '/>,document.getElementById('test'))
</script>
边栏推荐
- Leetcode62. 不同路径
- pycharm配置运行环境
- 小程序editor富文本编辑使用及rich-text解析富文本
- field injection is not recommended 的解决办法
- What is a driver signature and how does the driver get a digital signature?
- Ape anthropology topic 20
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (3. Kingbasees migration capability support system)
- 毕业三年之际写给可能迷茫的你我[转]
- Oracle创建表空间和用户
- EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
猜你喜欢
脲酶丨Worthington杰克豆脲酶的特性及测定方案
Solve thread safety problems & singleton mode
失败率高达80%,数字化转型如何正确完成战略规划?
【C】逆序字符串(俩种递归思路)
尿酸酶丨Worthington猪肝尿酸酶的特征:
CANoe应用案例之DoIP通信
Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan
通配符 SSL/TLS 证书
PHP 海报二维码合成
深度之眼(十八)——偏导数
随机推荐
以JSP为视图解析器搭建SSM项目
剑指 Offer 55 - I. 二叉树的深度
Jincang database kingbasees client programming interface guide ODBC (2. Overview)
数仓:Doris在美团的应用实践
通配符 SSL/TLS 证书
电商数据模型设计
Worthington丨Worthington胰蛋白酶抑制剂说明书
什么是驱动程序签名,驱动程序如何获取数字签名?
Oracle创建表空间和用户
深度之眼(十八)——偏导数
台式机dp接口在哪(主机没有dp接口怎么办)
Yolov5 learning notes (I) -- principle overview
使用Pytorch快速训练网络模型
SAP 临时表空间错误处理
Deep analysis of integrated learning gbdt
Worthington -- Specification of Worthington trypsin inhibitor
Where is the DP interface of desktop computer (what if the host has no DP interface)
PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM
多传感器融合定位(二)——基于地图的定位
Arm-a53 data "recommended collection"