React事件处理
<button
className = {styles.button}
onClick = {(e) => {
this.setState({ isOpen: !this.state.isOpen });
}}
>
购物车2件
</button>
给any添加自动映射
tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false
}
}
添加事件处理函数
(1) 箭头函数
handleClick = (e: React.MouseEvent<HTMLButtonElement, MoueEvent>) => {
this.setState({ isOpen: !this.state.isOpen })
}
render()
return (
<button
className = {styles.button}
onClick = {this.handleClick}
>
购物车2件
</button>
)
(2) 普通函数绑定this