在线一区二区三区高清视频,国产精品妇女一二三区,美女被遭强高潮网站在线播放,实拍各种胸走光见奶头

React組件實(shí)例的三大核心屬性refs

時(shí)間:2023-02-01 20:52:28 類(lèi)型:React
字號(hào):    

一. 字符串形式調(diào)用(簡(jiǎn)單,但是效率低,在未來(lái)的版本中可能會(huì)廢棄,不推薦使用)

 //創(chuàng)建類(lèi)式組件
        class Demo extends React.Component{
            render(){
                return (
                    <div>
                        <input type='text' placeholder="點(diǎn)擊按鈕提示" id="aa"/>&nbsp;
                        <button onClick={this.showData}>顯示提示效果</button>&nbsp;
                        <input ref='aa1' type='text' onBlur={this.showData1} placeholder="失去按鈕提示" id="aa1"/>
                    </div>
                    /* 組件內(nèi)的標(biāo)簽可以定義ref來(lái)標(biāo)識(shí)自己,會(huì)在組件的實(shí)例對(duì)象的this.refs屬性中找到 */
                )
            }
            showData = ()=>{
                let v = document.querySelector("#aa").value;
                alert(v);
            }
            showData1 = ()=>{
                let v = document.querySelector("#aa1").value;
                console.log(this);
                let v1 = this.refs.aa1.value;
                console.log(v,"--",v1);
                /*不建議使用它,因?yàn)?nbsp;string 類(lèi)型的 refs 存在 一些問(wèn)題。它已過(guò)時(shí)并可能會(huì)在未來(lái)的版本被移除。
                   一句話總結(jié): 效率不高
                */
            }
        }
        //渲染組件到頁(yè)面
        ReactDOM.render(<Demo/>, document.querySelector("#test"));

二. 回調(diào)函數(shù)調(diào)用(簡(jiǎn)單好用,使用頻率高)

//創(chuàng)建類(lèi)式組件
        class Demo extends React.Component{
            render(){
                return (
                    <div>
                        <input ref={(currentNode)=>{this.input1=currentNode}} type='text' placeholder="點(diǎn)擊按鈕提示" id="aa"/>&nbsp;
                        <button onClick={this.showData}>顯示提示效果</button>&nbsp;
                        <input ref={c=>this.input2=c} type='text' onBlur={this.showData1} placeholder="失去按鈕提示" id="aa1"/>
                    </div>
                    /* 以上兩種ref的賦值方式屬于: 回調(diào)函數(shù) */
                )
            }
            showData = ()=>{
                 console.log(this.input1.value);
            }
            showData1 = ()=>{
                console.log(this.input2.value);
            }
        }

二. React.createRef() 調(diào)用(官方推薦)

//創(chuàng)建類(lèi)式組件
 //創(chuàng)建類(lèi)式組件
        class Demo extends React.Component{
            myRef = React.createRef()
            myRef2 = React.createRef()
            //React.createRef調(diào)用后可以返回一個(gè)容器,該容器可以存儲(chǔ)被ref所標(biāo)識(shí)的節(jié)點(diǎn),該容器是"專(zhuān)人專(zhuān)用"
            //推薦 ref方法
            render(){
                return (
                    <div>
                        <input ref={this.myRef} type='text' placeholder="點(diǎn)擊按鈕提示" id="aa"/>&nbsp;
                        <button onClick={this.showData}>顯示提示效果</button>&nbsp;
                        <input ref={this.myRef2} type='text' onBlur={this.showData1} placeholder="失去按鈕提示" id="aa1"/>
                    </div>
                   
                )
            }
            showData = ()=>{
                 let input1 = this.myRef.current;
                 console.log(input1.value);
            }
            showData1 = ()=>{
             
               console.log( this.myRef2.current.value);
            }
        }


<