React的Ref三中使用方式:
import React from 'react'
export default class RefDemo extends React.Component {
constructor() {
super()
this.objRef = React.createRef()
// { current: null }
}
componentDidMount() {
// console.log(`span1: ${this.refs.ref1.textContent}`)
// console.log(`span2: ${this.ref2.textContent}`)
// console.log(`span3: ${this.ref3.current.textContent}`)
setTimeout(() => {
this.refs.stringRef.textContent = 'string ref got'
this.methodRef.textContent = 'method ref got'
this.objRef.current.textContent = 'obj ref got'
}, 1000)
}
render() {
return (
<>
<p ref="stringRef">span1</p> //第一种使用方式
<p ref={ele => (this.methodRef = ele)}>span3</p> //第二种使用方式
<p ref={this.objRef}>span3</p> //第三种使用方式
</>
)
}
}