pakhozou
论坛版主
论坛版主
  • 最后登录2023-03-16
  • 发帖数21
  • 社区居民
  • 忠实会员
阅读:3165回复:0

React ref获取值

楼主#
更多 发布于:2020-06-29 09:11
import React from 'react';  //导入react
//xxx 组件名
class demo extends React.Component {
constructor(props){
  super(props);
  this.add = this.add.bind(this)
}
  add(e){
  e.preventDefault();
    let username = this.username.value
    console.log(username)
  }
//渲染
    render() {
        return (
            <div>
              <input type="text" ref={name=>{this.username = name }}/>
              <button onClick={this.add}>确定</button>
            </div>
        )
    }
}
export {demo as default}
游客


返回顶部

公众号

公众号