react組件實(shí)例屬性props實(shí)例詳解
react組件實(shí)例屬性props
props
props簡單使用
class Person extends React.Component {
render() {
return (
<ul>
<li>姓名:{this.props.name}</li>
<li>年齡:{this.props.age}</li>
<li>性別:{this.props.sex}</li>
</ul>
)
}
}
const root = ReactDOM.createRoot(document.getElementById('test'));
// 這里props屬性要寫成key:"value"形式,但是會默認(rèn)將value視為字符串,若想傳遞js類型的字面量,則要加{}
root.render(<Person name="kl" age={19} sex="男" />);props批量操作
class Person extends React.Component {
render() {
return (
<ul>
<li>姓名:{this.props.name}</li>
<li>年齡:{this.props.age}</li>
<li>性別:{this.props.sex}</li>
</ul>
)
}
}
const root = ReactDOM.createRoot(document.getElementById('test'));
const p = { name: "lml", sex: "nan", age: 18 }
root.render(<Person {...p} />);props屬性類型限制
需要導(dǎo)入prop-type
https://unpkg.com/prop-types@15.6/prop-types.js
class Person extends React.Component {
render() {
return (
<ul>
<li>姓名:{this.props.name}</li>
<li>年齡:{this.props.age + 1}</li>
<li>性別:{this.props.sex}</li>
</ul>
)
}
}
// 對props限制
Person.propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speak: PropTypes.func, // 限制為函數(shù)
}
// props默認(rèn)值
Person.defaultProps = {
sex: '不男不女',
age: 18,
}
function speak() {
console.log('說話了');
}
const root = ReactDOM.createRoot(document.getElementById('test'));
// 這里props屬性要寫成key:"value"形式,但是會默認(rèn)將value視為字符串,若想傳遞js類型的字面量,則要加{}
root.render(<Person name="lml" age={19} speak={speak} />);props屬性限制的簡寫
class Person extends React.Component {
// 對props限制
static propTypes = {
name: PropTypes.string.isRequired,
sex: PropTypes.string,
age: PropTypes.number,
speak: PropTypes.func, // 限制為函數(shù)
}
// props默認(rèn)值
static defaultProps = {
sex: '不男不女',
age: 18,
}
render() {
return (
<ul>
<li>姓名:{this.props.name}</li>
<li>年齡:{this.props.age + 1}</li>
<li>性別:{this.props.sex}</li>
</ul>
)
}
}
const root = ReactDOM.createRoot(document.getElementById('test'));
root.render(<Person name="lml" age={19} />);函數(shù)組件使用props
function People(props) {
return (
<ul>
<li>name:{props.name}</li>
<li>age:{props.age}</li>
</ul>
)
}
const root = ReactDOM.createRoot(document.getElementById('test'));
root.render(<People name="lml" age={19} />);補(bǔ)充:React之組件實(shí)例的三大屬性之props
props
每個組件對象都會有props(properties的簡寫)屬性
組件標(biāo)簽的所有屬性都保存在props中
通過標(biāo)簽屬性從組件外向組件內(nèi)傳遞變化的數(shù)據(jù),組件內(nèi)不能夠修改props數(shù)據(jù)
實(shí)例
做一個能夠展示姓名、學(xué)號、性別的列表組件,并且能夠設(shè)置默認(rèn)值。
這里就需要用到之前沒用到的prop-types.js了
為了防止react太笨重,就將限制類型的模塊單獨(dú)抽了出來,方便選擇是否使用,需要使用導(dǎo)入即可。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<div id="test4"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Person extends React.Component {
//對標(biāo)簽屬性進(jìn)行類型、必要性的限制
static propTypes = {
name:PropTypes.string.isRequired, //限制name必傳,且為字符串
sex:PropTypes.string,
age:PropTypes.number, //限制為數(shù)字
speak:PropTypes.func //限制為函數(shù)
}
//指定模標(biāo)簽屬性值
static defaultProps ={
sex:"男",
age:18
}
render() {
//props只讀不能改
const {name, age, gender} = this.props
return (
<ul>
<li>{name}</li>
<li>{age}</li>
<li>{gender}</li>
</ul>
)
}
}
ReactDOM.render(<Person name="tom" age={19} gender="男"/>, document.getElementById("test1"))
ReactDOM.render(<Person name="pretty" gender="女"/>, document.getElementById("test2"))
ReactDOM.render(<Person name="hunter" age={21} gender="男"/>, document.getElementById("test3"))
const p = {name:"AABB", age:19, gender:"UNK"}
//展開運(yùn)算符
ReactDOM.render(<Person {...p}/>, document.getElementById("test4"))
function speak(){
console.log("speaking...")
}
</script>
</body>
</html>前言萬語都在注釋里,運(yùn)行結(jié)果:

如果我們將AABB的age改成字符型
const p = {name:"AABB", age:"19", gender:"UNK"}那么結(jié)果如下:

會警告數(shù)據(jù)類型錯誤,達(dá)到限制數(shù)據(jù)類型的提示。
到此這篇關(guān)于react組件實(shí)例屬性props的文章就介紹到這了,更多相關(guān)react組件實(shí)例屬性props內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React-hook-form-mui基本使用教程(入門篇)
react-hook-form-mui可以幫助開發(fā)人員更輕松地構(gòu)建表單,它結(jié)合了React?Hook?Form和Material-UI組件庫,使用react-hook-form-mui,開發(fā)人員可以更快速地構(gòu)建表單,并且可以輕松地進(jìn)行表單驗證和數(shù)據(jù)處理,本文介紹React-hook-form-mui基本使用,感興趣的朋友一起看看吧2024-02-02
關(guān)于React動態(tài)修改元素樣式的三種方式
這篇文章主要介紹了關(guān)于React動態(tài)修改元素樣式的三種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
webpack打包react項目的實(shí)現(xiàn)方法
這篇文章主要介紹了webpack打包react項目的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

