ref用法(获取元素节点)

import React from 'react'


export default class App extends React.Component {

    myref=React.createRef()

    render() {
        return (
            
            <div>
                {/* 获取输入框的内容 写法一 */}
                <input ref="mytext"></input>
                  <button onClick={
                    ()=>{
                        console.log(this.refs.mytext.value)   
                    }
                }>add 匿名函数触发</button>


                {/* 获取输入框的内容 写法二 */}
                <input ref={this.myref}></input>
                  <button onClick={
                    ()=>{
                        console.log(this.myref.current.value)   
                    }
                }>add 匿名函数触发</button>
            </div>
        )
    }
}

 

posted @ 2023-04-24 21:46  凯宾斯基  阅读(91)  评论(0)    收藏  举报