我已经开始学习 React,但我无法理解为什么 console.log 会记录两次。下面是我的代码,当我在 chrome 中检查它时,它会显示“随机文本”这个词两次而不是一次。
import React, { Component } from "react";
class Counter extends Component {
state = {
count: 0
};
render() {
console.log('random-text');
return (
<div>
<span className={this.getBaadgeClasses()}>{this.formatCount()}</span>
<button
onClick={this.handleIncrement}
className="btn btn-success m-2"
>
Increment
</button>
{/* <ul>
{this.state.tags.map((tag) => (
<li key={tag}>{tag}</li>
))}
</ul> */}
</div>
);
}
getBaadgeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}
formatCount() {
const { count } = this.state;
return count === 0 ? "Zero" : count;
}
handleIncrement = () => {
this.setState({count: this.state.count + 1})
}
}
export default Counter;