reactjs-如何设置backgroundcolor的内联样式? [英] reactjs - how to set inline style of backgroundcolor?

查看:46
本文介绍了reactjs-如何设置backgroundcolor的内联样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置某些元素的样式属性,但是语法不正确.谁能建议我错了?

I want to set the style properties of some elements but I don't have the syntax correct. Can anyone suggest where I am wrong?

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

}

更新:对于任何查看此内容的人,请查看注释,这不是有效的代码.

UPDATE: for anyone looking at this please see comments this is not working code.

推荐答案

https://facebook.github.io/react/tips/inline-styles.html

您不需要引号.

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

这篇关于reactjs-如何设置backgroundcolor的内联样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆