LikeButton.jsx
import React from 'react';
import ReactDOM from 'react-dom';
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
handleClick(event) {
const nextCount = !this.state.liked;
this.setState({ liked: nextCount });
}
render() {
var text = this.state.liked ? '喜欢' : '不喜欢';
return (
<p onClick={this.handleClick}>
你<b>{text}</b>我。点我切换状态。
</p>
);
}
}
export default LikeButton;
onClick={this.handleClick}改为onClick={this.handleClick.bind(this)}