[英]How do I render conditional styles (React + Sass)
I have these classes in my scss
file: 我的
scss
文件中有这些类:
.errorNotice {
display: none;
font-size: 12px;
color: #D85B5F;
background: white;
padding: 5px;
.error & {
display: inline-block;
}
}
And in my render
function, 在我的
render
功能中
{this.state && this.state.error ?
(
<p className={styles.errorNotice + (this.state.error ? styles.error : '')}>{this.state.error}</p>
) :
null
}
This isn't giving me the styles I want though. 虽然这没有给我我想要的样式。 It's giving me a weird concatenation:
这给了我一个奇怪的连接:
campaigns-components-WaitlistForm-styles_errorNotice-1X-Ty4campaigns-components-WaitlistForm-styles_error-JNXx2E
My goal is to display the errorNotice
with the correct display
attribute. 我的目标是使用正确的
display
属性显示errorNotice
。
Thanks 谢谢
Use string literals to combine strings. 使用字符串文字来组合字符串。 In classname you just need the name of the classes.
在类名中,您只需要类的名称。 So with the ` you can start a string literal and inside of
${}
you can run javascript code and return something: 因此,使用`可以启动字符串文字,并且可以在
${}
内运行javascript代码并返回以下内容:
<p className={`errorNotice ${this.state.error ? "error" : ''}`}>{this.state.error}</p>
Maybe it's also an option for you to have a look at styled-components( https://www.styled-components.com ). 也许您也可以选择查看styled-components( https://www.styled-components.com )。 IMHO for React projects this is really neat to use and you don't have to create special css-files anymore or take care of classnames.
IMHO for React项目真的非常好用,您不必再创建特殊的css文件或使用类名了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.