[英]Strange condition rendering failed in jsx
I have a this component that set a variable to the background image, this won't work 我有一个this组件,它将一个变量设置为背景图像,这不起作用
render() {
const target_role_gravatar = this.props.target_role_gravatar
return(
<div className="logo"
style={{"backgroundImage": "url(" + target_role_gravatar ? target_role_gravatar : 'something else' + ")"}}>
</div>
)
}
I got all value of target_role_gravatar. 我得到了target_role_gravatar的所有值。
But this worked. 但这行得通。
render() {
const target_role_gravatar = this.props.target_role_gravatar
return(
<div>
{target_role_gravatar && <div className="logo"
style={{"backgroundImage": "url(" + target_role_gravatar + ")"}}>
</div>}
{!target_role_gravatar && <div className="logo"
style={{"backgroundImage": "url(" + this.logo + ")"}}>
</div>}
</div>
)
}
Why? 为什么?
+
has a higher precedence than ?:
. +
的优先级比?:
。 Your expression is evaluated as 您的表情被评估为
(prefix + condition) ? yes : (no + suffix)
Solution: Use parenthesis to change the precedence: 解决方案:使用括号更改优先级:
"url(" + (target_role_gravatar ? target_role_gravatar : 'something else') + ")"
Or use template literals: 或使用模板文字:
`url(${target_role_gravatar ? target_role_gravatar : 'something else'})`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.