React学习记录——ref
程序员文章站
2022-07-03 09:02:20
...
import React from 'react'
import classNames from 'classnames'
interface State {
count: number,
className: string
}
interface Prop {
}
export default class ReButton extends React.Component<Prop, State> {
myRef: any
constructor(props: any) {
super(props)
this.add = this.add.bind(this)
this.myRef = React.createRef<any>()
this.state = {
count: 0,
className: ''
}
}
add(e: any) {
e.preventDefault()
let className = classNames({
"re-button": this.state.count % 2 === 1
})
this.setState({
count: this.state.count + 1,
className: className
})
console.log(this.myRef)
}
render() {
return (
<div>
<button ref={this.myRef} className={this.state.className} onClick={this.add}>测试</button>
<div>
{this.state.count}
</div>
</div>
)
}
}
输出:
上一篇: 几道动态规划算法题
推荐阅读
-
React Native学习教程之自定义NavigationBar详解
-
React Native学习教程之Modal控件自定义弹出View详解
-
React Native学习教程之自定义NavigationBar详解
-
React Native学习教程之Modal控件自定义弹出View详解
-
React Native学习之Android的返回键BackAndroid详解
-
sql cast,convert,QUOTENAME,exec 函数学习记录
-
通过实例学习React中事件节流防抖
-
深入学习TypeScript 、React、 Redux和Ant-Design的最佳实践
-
React Native学习之Android的返回键BackAndroid详解
-
Node学习记录之cluster模块