最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react組件實(shí)例屬性props實(shí)例詳解

 更新時(shí)間:2023年01月30日 09:07:32   作者:K-L  
這篇文章主要介紹了react組件實(shí)例屬性props,本文結(jié)合實(shí)例代碼給大家簡單介紹了props使用方法,代碼簡單易懂,需要的朋友可以參考下

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

每個(gè)組件對象都會有props(properties的簡寫)屬性
組件標(biāo)簽的所有屬性都保存在props中
通過標(biāo)簽屬性從組件外向組件內(nèi)傳遞變化的數(shù)據(jù),組件內(nèi)不能夠修改props數(shù)據(jù)

實(shí)例

做一個(gè)能夠展示姓名、學(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ù)類型錯(cuò)誤,達(dá)到限制數(shù)據(jù)類型的提示。

到此這篇關(guān)于react組件實(shí)例屬性props的文章就介紹到這了,更多相關(guān)react組件實(shí)例屬性props內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React 實(shí)現(xiàn)井字棋的示例代碼

    React 實(shí)現(xiàn)井字棋的示例代碼

    本文主要介紹了React 實(shí)現(xiàn)井字棋,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • React的diff算法核心復(fù)用圖文詳解

    React的diff算法核心復(fù)用圖文詳解

    這篇文章主要為大家介紹了React的diff算法核心復(fù)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Rect Intersection判斷兩個(gè)矩形是否相交

    Rect Intersection判斷兩個(gè)矩形是否相交

    這篇文章主要為大家介紹了Rect Intersection判斷兩個(gè)矩形是否相交的算法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 一起來了解React的Hook

    一起來了解React的Hook

    這篇文章主要為大家詳細(xì)介紹了React的Hook,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • React-hook-form-mui基本使用教程(入門篇)

    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)行表單驗(yàn)證和數(shù)據(jù)處理,本文介紹React-hook-form-mui基本使用,感興趣的朋友一起看看吧
    2024-02-02
  • 使用hooks寫React組件需要注意的5個(gè)地方

    使用hooks寫React組件需要注意的5個(gè)地方

    這篇文章主要介紹了使用hooks寫React組件需要注意的5個(gè)地方,幫助大家更好的理解和學(xué)習(xí)使用React組件,感興趣的朋友可以了解下
    2021-04-04
  • react實(shí)現(xiàn)換膚功能的示例代碼

    react實(shí)現(xiàn)換膚功能的示例代碼

    這篇文章主要介紹了react實(shí)現(xiàn)換膚功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • 關(guān)于React動(dòng)態(tài)修改元素樣式的三種方式

    關(guān)于React動(dòng)態(tài)修改元素樣式的三種方式

    這篇文章主要介紹了關(guān)于React動(dòng)態(tài)修改元素樣式的三種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • JavaScript中React 面向組件編程(下)

    JavaScript中React 面向組件編程(下)

    在React面向組件編程中,除了上一章節(jié)的組件實(shí)例的三大核心屬性以外,還有很多重要的內(nèi)容比如:React 的生命周期,受控組件與非受控組件,高階函數(shù)和函數(shù)柯里化的理解等,在本文中會給大家繼續(xù)講解React 面向組件編程中剩余的內(nèi)容
    2023-03-03
  • webpack打包react項(xiàng)目的實(shí)現(xiàn)方法

    webpack打包react項(xiàng)目的實(shí)現(xiàn)方法

    這篇文章主要介紹了webpack打包react項(xiàng)目的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06

最新評論

淅川县| 绍兴市| 铅山县| 雷州市| 霍邱县| 北宁市| 屏东市| 项城市| 湟中县| 石城县| 张掖市| 阳曲县| 马公市| 天峻县| 襄垣县| 常熟市| 莱州市| 思南县| 平乡县| 尤溪县| 安溪县| 澄江县| 堆龙德庆县| 临颍县| 桂东县| 西峡县| 永和县| 高平市| 德钦县| 利津县| 塔河县| 左云县| 彝良县| 清远市| 宽城| 合水县| 正镶白旗| 福海县| 吉安县| 巴里| 新密市|