我想设置一些元素的样式属性,但语法不正确。谁能指出我错在哪里?
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>
);
}
}
更新:对于任何查看此代码的人,请查看评论,这不是工作代码。
最佳答案
https://facebook.github.io/react/tips/inline-styles.html
你不需要引号。
<a style={{backgroundColor: bgColors.Yellow}}>yellow</a>
关于javascript - reactjs - 如何设置背景颜色的内联样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043436/