当前位置:网站首页>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>
边栏推荐
- GhostNets on Heterogeneous Devices via Cheap Operations
- 【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
- ISO 13400(DoIP)标准解读
- leetcode 763. Partition Labels 划分字母区间(中等)
- Connection pool - return connection details (Part 2)
- SAP temporary tablespace error handling
- Best practices for migration of kingbasees v8.3 to v8.6 of Jincang database (2. Compatibility of kingbasees v8.3 and v8.6)
- DevOps在物联网解决方案中的应用
- Compatibility description between kingbasees and Oracle (4. SQL)
- Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
猜你喜欢
随机推荐
GhostNets on Heterogeneous Devices via Cheap Operations
尿酸酶丨Worthington猪肝尿酸酶的特征:
Solve thread safety problems & singleton mode
With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally
多传感器融合定位(二)——基于地图的定位
小程序editor富文本编辑使用及rich-text解析富文本
EN 1935建筑五金.单轴铰链—CE认证
数仓:Doris在美团的应用实践
pycharm配置运行环境
CANoe应用案例之DoIP通信
Compatibility description between kingbasees and Oracle (5. Pl/sql)
leetcode 763. Partition Labels 划分字母区间(中等)
Kingbasees client programming interface guide ODBC (4. Create data source)
DevOps在物联网解决方案中的应用
什么是驱动程序签名,驱动程序如何获取数字签名?
C语言 n*n矩阵求值及求逆矩阵[通俗易懂]
Create AP hotspots for imx6 development board QT system based on rtl8723 cross compile iptables
【C】atoi和offsetof的介绍和模拟实现
Pagoda phpMyAdmin unauthorized access vulnerability
以JSP为视图解析器搭建SSM项目








