import React from 'react'
class App extends React.Component {
private readonly myRef: React.RefObject<HTMLDivElement>
constructor (props: any) {
super(props)
this.myRef = React.createRef() // 1.创建一个ref的引用,并且可以通过this.myRef属性去进行访问
this.state = {
}
}
componentDidMount () {
// 可以通过this.getRef.current 属性名来进行获取
console.log(this)
}
render () {
return (
<div ref={this.myRef}>
<h1>234234324</h1>
</div>
)
}
}
export default App
react函数式组件+ts+ref
于 2022-10-19 17:28:11 首次发布