文章目录
ref
ref:给表单元素进行标记,为了更好获取元素,类似于js中的
document.getElementsByTagName
这类的获取元素
基本使用
//创建组件
class Demo extends React.Component{
//展示左侧输入框的数据
showData = ()=>{
const {
input1} = this
alert(input1.value)
}
//展示右侧输入框的数据
showData2 = ()=>{
const {
input2} = this
alert(input2.value)
}
render(){
return(
<div>
<input ref={
c => this.input1 = c } type="text" placeholder="点击按钮提示数据"/>
<button onClick={
this.showData}>点我提示左侧的数据</button