ref
- 给子元素绑定ref属性,这样父元素就能得到绑定了ref属性的元素/组件
- refs获取 得到的是一个集合
- refs要通过事件或者生命周期获取
showRef=()=>{
console.log(this.refs)
}
render() {
return (
<div>
父组件
<button onClick={this.showRef}>获取ref</button>
<Mother ref='mother' ></Mother>
</div>
)
}
import React, { Component,createRef } from 'react'
btn=createRef()
showRef=()=>{
console.log(this.btn)
}
render() {
return (
<div>
<button onClick={this.showRef}>获取ref</button>
<Mother ref={this.btn} ></Mother>
</div>
)
}
change=()=>{
console.log(this.inp)
}
render() {
return (
<div>
<input
type='text'
onChange={this.change}
ref={inp=>{this.inp=inp}}/>
</div>
)
}