【发布时间】:2017-03-16 12:13:18
【问题描述】:
我现在正在浏览 ReactJS 教程,仔细考虑“在 React 中思考”部分,特别是代码的一个方面真的让我很困扰:
class ProductRow extends React.Component {
render() {
var name = this.props.product.stocked ?
this.props.product.name :
<span style={{color: 'red', background: 'green'}}> // this line
{this.props.product.name}
</span>;
return (
<tr>
<td>{name}</td>
<td>{this.props.product.price}</td>
</tr>
);
}
}
其余代码见:http://codepen.io/lacker/pen/vXpAgj
...在第 11 行,样式被指定为 {color: 'red'}。在我看来,这不应该工作,因为它不符合样式语法(例如“颜色:'红色'”)。然而,如果我用双引号替换大括号,代码就不会运行。
1) 是否有将对象文字转换为双引号格式的 ES6/JSX/React 规则? (这是如何工作的?)
2) 为什么用双引号替换大括号不起作用? (在codepen上试过)
谢谢!
【问题讨论】:
标签: javascript css reactjs ecmascript-6 babeljs