reactjs -如何设置backgroundcolor的内联样式?

lmvvr0a8  于 2022-11-04  发布在  React
关注(0)|答案(4)|浏览(139)

我想设置一些元素的样式属性,但语法不正确。有人能指出我哪里错了吗?

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>
           );
  }

}

更新:对于任何人在看这个,请看到评论,这是不工作的代码。

nqwrtyyt

nqwrtyyt1#

https://facebook.github.io/react/tips/inline-styles.html
你不需要引用。

<a style={{backgroundColor: bgColors.Yellow}}>yellow</a>
carvr3hs

carvr3hs2#

你的引号位置不对。下面是一个简单的例子:

<div style={{backgroundColor: "#FF0000"}}>red</div>
j9per5c4

j9per5c43#

如果你想要多个样式,这是正确的完整答案。这是带有类和样式的div:

<div className="class-example" style={{width: '300px', height: '150px'}}></div>
r9f1avp5

r9f1avp54#

<a style={{ backgroundColor: bgColors.Default }}>default</a>

相关问题