午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

react中ref獲取dom或者組件的實現方法

瀏覽:673日期:2022-06-10 09:23:24
目錄
  • react中ref獲取dom或者組件方法
    • 使用ref獲取DOM的引用
    • 使用ref獲取組件的引用
  • react中的三種ref獲取DOM節點
    • 第一種 ref字符串方式獲取Dom節點方式
    • 第二種 回調式獲取Dom節點方式
    • 第三種 回調式獲取Dom節點方式 掛在到自身實例
  • 總結

    react中ref獲取dom或者組件方法

    使用ref獲取DOM的引用

    在vue中,如果想獲取DOM元素時,可以使用this.$refs.引用名稱

    在react中也可以像vue中,有類似的寫法,如下

    為元素添加ref引用

    <h2 ref="test">這是h2標簽</h2>

    在頁面上獲取元素

    this.refs.test

    使用ref獲取組件的引用

    為組件添加ref引用

    <Text ref="hellow"/>

    在頁面上使用組件的引用

    this.refs.hellow

    注意點: 只要使用ref拿到組件的引用對象,它就是組件的實例對象,因此就可以調用這個組件的方法,或者它的屬性

    react中的三種ref獲取DOM節點

    第一種 ref字符串方式獲取Dom節點方式

    已廢棄的原始方法

    ?? ? class Dom extends React.Component{
    ? ? showInputDom = () =>{
    ? ? ? const {userNameInput} = this.refs
    ? ? ? console.log(userNameInput);
    ? ? }
    ? ? render(){
    ? ? ? return (
    ? ? ? ? <div>
    ? ? ? ? ? <input ref="userNameInput" type="text"/>
    ? ? ? ? ? <button onClick={this.showInputDom}>點擊顯示inpuDom</button>
    ? ? ? ? </div>
    ? ? ? )
    ? ? }
    ? }
    ? ReactDOM.render(<Dom/>,document.getElementById("root"))

    第二種 回調式獲取Dom節點方式

    開發常用

    ?? ?class Dom extends React.Component{
    ? ? showInputDom = () =>{
    ? ? ? const {userNameInput} = this
    ? ? ? console.log(userNameInput);
    ? ? }
    ? ? render(){
    ? ? ? return (
    ? ? ? ? <div>
    ? ? ? ? ? {/*注釋 (currentNode)=>{this.userNameInput =currentNode} 這里邊的currentNode 為 當前的node節點 簡稱c */}
    ? ? ? ? ? {/*<input ref={(currentNode)=>{this.userNameInput =currentNode}} type="text"/>*/}
    ? ? ? ? ? <input ref={(c)=>{this.userNameInput = c}} type="text"/>
    ? ? ? ? ? <button onClick={this.showInputDom}>點擊顯示inpuDom</button>
    ? ? ? ? </div>
    ? ? ? )
    ? ? }
    ? }
    ? ReactDOM.render(<Dom/>,document.getElementById("root"))

    第三種 回調式獲取Dom節點方式 掛在到自身實例

    ?? ? class Dom extends React.Component{
    ? ? // 掛載到了自身實例上了
    ? ? userNameInput= (c) =>{
    ? ? ? this.input1 = c ;
    ? ? ? console.log(c);
    ? ? }
    ? ? render(){
    ? ? ? return (
    ? ? ? ? <div>
    ? ? ? ? ? {/*會在試圖更新時調用兩次 第一次賦值為null,第二次賦值為dom節點*/}
    ? ? ? ? ? {/*<input ref={(c)=>{this.userNameInput =c}} type="text"/>*/}
    ? ? ? ? ? {/*在試圖更新時不會調用}
    ? ? ? ? ? {/*<input ref={ this.userNameInput } type="text"/>*/}
    ? ? ? ? ? {/*注意這倆個方法是有區別的,這兩個對項目的影響可以忽略不記*/}
    ? ? ? ? ? <input ref={this.userNameInput} type="text"/>
    ? ? ? ? ? <button onClick={this.showInputDom}>點擊顯示inpuDom</button>
    ? ? ? ? </div>
    ? ? ? )
    ? ? }
    ? }
    ? ReactDOM.render(<Dom/>,document.getElementById("root"))

    總結

    以上為個人經驗,希望能給大家一個參考,也希望大家多多支持。

    標簽: JavaScript
    主站蜘蛛池模板: 大地资源1080在线观看 | 黄色一级免费网站 | 成人高清网站 | 黄色a免费| 日韩精品在线一区二区三区 | 国产成人免费看一级大黄 | 爱爱久久| 午夜影院操| 毛片视频网址 | 在线精品亚洲欧美日韩国产 | 四虎影视av | 成人超碰在线 | 69精品久久| 久久99久久99精品免观看粉嫩 | 在线黄色免费 | 一级激情片 | www五月婷婷 | 黄色大毛片 | 日韩一级黄色 | 亚洲欧美日韩综合在线 | 最新中文字幕 | xxxx久久| 麻豆久久久久 | 国产黄色免费大片 | 狠狠干夜夜| av免费毛片| 亚洲欧美片 | 国产精品色婷婷99久久精品 | 成人在线播放网站 | 天天舔天天操天天干 | 亚洲高清视频在线播放 | 天堂在线观看中文字幕 | 欧美精品乱码99久久蜜桃 | 国产欧美自拍 | 欧美中文字幕在线观看 | 十大污网站| 99国产精品一区二区 | 欧美日韩国产不卡 | 久色视频在线 | av最新网址| 成人在线免费观看网址 |