久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

react中ref獲取dom或者組件的實現(xiàn)方法

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

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

    使用ref獲取DOM的引用

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

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

    為元素添加ref引用

    <h2 ref="test">這是h2標(biāo)簽</h2>

    在頁面上獲取元素

    this.refs.test

    使用ref獲取組件的引用

    為組件添加ref引用

    <Text ref="hellow"/>

    在頁面上使用組件的引用

    this.refs.hellow

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

    react中的三種ref獲取DOM節(jié)點

    第一種 ref字符串方式獲取Dom節(jié)點方式

    已廢棄的原始方法

    ?? ? 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"))

    第二種 回調(diào)式獲取Dom節(jié)點方式

    開發(fā)常用

    ?? ?class Dom extends React.Component{
    ? ? showInputDom = () =>{
    ? ? ? const {userNameInput} = this
    ? ? ? console.log(userNameInput);
    ? ? }
    ? ? render(){
    ? ? ? return (
    ? ? ? ? <div>
    ? ? ? ? ? {/*注釋 (currentNode)=>{this.userNameInput =currentNode} 這里邊的currentNode 為 當(dāng)前的node節(jié)點 簡稱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"))

    第三種 回調(diào)式獲取Dom節(jié)點方式 掛在到自身實例

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

    總結(jié)

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

    標(biāo)簽: JavaScript
    主站蜘蛛池模板: 泰和县| 枣强县| 扎赉特旗| 峨边| 宜阳县| 自贡市| 嵊州市| 中方县| 临高县| 霍邱县| 关岭| 滨海县| 陇南市| 平邑县| 娱乐| 平远县| 壶关县| 新化县| 桐梓县| 迁西县| 罗田县| 布尔津县| 双牌县| 贵南县| 景宁| 兴和县| 林甸县| 广昌县| 焦作市| 兴宁市| 万山特区| 高碑店市| 东宁县| 永清县| 固安县| 祥云县| 重庆市| 临海市| 沙洋县| 穆棱市| 黔江区|