【发布时间】:2021-01-13 16:17:46
【问题描述】:
我正在学习 ReactJs 并提出了动态渲染类
render() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return (
<div>
<span className={classes}>{this.formatCount()}</span>
<button className="btn btn-secondary btn-sm">Increment</button>
</div>
);
想知道 classes += this.state.count ===0 如何? “警告”:“主要”; 工作过
我的意思是 x += y;应该是 x = x + y; 为什么要使用 this.state.count 添加类
我在这里应用的逻辑是
this.state.count === 0 ? classes = "badge m-2 badge-warning" : classes = "badge m-2 badge-primary";
【问题讨论】:
-
x += y;和 x = x + y 都是一样的,将 x 添加到 y 并将其分配给 x,参考下面的链接,它可以帮助您进一步理解 Javascript 中的运算符:只是 x += y 是一种更短的方式; w3schools.com/js/js_operators.asp