【发布时间】:2017-06-06 16:29:23
【问题描述】:
我想设置一些元素的样式属性,但语法不正确。谁能建议我错在哪里?
import React from 'react';
import debug from 'debug'
const log = debug('app:component:Header');
var bgColors = { "Default": "#81b71a",
"Blue": "#00B1E1",
"Cyan": "#37BC9B",
"Green": "#8CC152",
"Red": "#E9573F",
"Yellow": "#F6BB42",
};
export default class SideBar extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<a style="{{backgroundColor: {bgColors.Default}}}" >default</a>
<a style="{{backgroundColor: {bgColors.Blue}}}" >blue</a>
<a style="{{backgroundColor: {bgColors.Cyan}}}" >cyan</a>
<a style="{{backgroundColor: {bgColors.Green}}}" >green</a>
<a style="{{backgroundColor: {bgColors.Red}}}" >red</a>
<a style="{{backgroundColor: {bgColors.Yellow}}}" >yellow</a>
);
}
}
更新:对于任何查看此内容的人,请参阅 cmets 这不是工作代码。
【问题讨论】:
-
这也行吗?我会认为你必须将这些锚点包装在一个元素中,因为返回应该是一个元素,而不是很多——除非你编辑了一些东西
-
它可能不起作用。我从一段更大的代码中破解了它。
-
这是正确的语法:
<p style= {{color:'gray'}}>{props.item.text}</p>
标签: javascript reactjs