简体   繁体   English

如何使用React.js制定样式

[英]How to enact styles with React.js

I am trying to use inline styles with React.js, but I keep running into errors: 我试图在React.js中使用内联样式,但是我一直遇到错误:

In my render function, I have: 在我的渲染函数中,我有:

render: function() {
    var style = this.state.submitted ? {{"backgroundColor": "#1abc9c", "opacity": "0.6"}} : {{}}; 
    return (
      <div>

        <h1 className="home-two-question" style={style}>{text}</h1>

      </div>
    )
  },

Basically I want to toggle this style on click. 基本上,我想在点击时切换此样式。 However, when I run this, I get an error from React.js. 但是,当我运行它时,我从React.js收到一个错误。 What is the correct syntax for inline styles in React.js? React.js中内联样式的正确语法是什么? Thank you! 谢谢!

In this line: 在这一行:

var style = this.state.submitted ? {{"backgroundColor": "#1abc9c", "opacity": "0.6"}} : {{}};

you're just in plain JavaScript, not inside of a JSX tag. 您只是使用普通的JavaScript,而不是JSX标签内。 Thus, you just want to use single {} , not double {{}} : 因此,您只想使用单个{} ,而不是双{{}}

var style = this.state.submitted ? {"backgroundColor": "#1abc9c", "opacity": "0.6"} : {};

In particular, when you do something like: 特别是当您执行以下操作时:

<div style={{"backgroundColor": "white"}}>

There is one set of {} to denote that the value of the style prop should be interpreted as JavaScript, and another set of {} to denote that you are constructing an object within that value. 一组{}表示应将style prop的值解释为JavaScript,而另一组{}表示要在该值内构造一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM