import React from "react";
class ClassCom extends React.Component {
state = {
name:'小红'
}
// 写法一:
handle = (val) =>{
// 有this指向, 指向当前组件
console.log(val, this);
}
// 写法二:
handleTwo(){
// 没有this指向,undefined
console.log(val, this);
}
render() {
// console.log(this);
return <div>
<div>class组件</div>
// 如果需要传参的话,就需要写成箭头函数
// 如果不需要传参直接写成this.handleTwo, 如果加了()就会直接触发,因此不要加
<button onClick={() => this.handle(this.state.name)}>点我1</button>
<button onClick={() => this.handleTwo(this.state.name)}>点我2</button>
</div>
}
}
export default ClassCom
09-07
107
08-19
873
05-23