我有一些HTML需要自定义属性,没有任何值取决于具体情况..例如这些都是有效的:
<div className="playlist-row" selected>
<div className="playlist-row" active>
<div className="playlist-row">
我尝试过像
这样的事情let customAttr = {'active'}; // incorrect syntax
let customAttr = {'active': true}; //doesn't work
let customAttr = {'active': ''}; //doesn't work
<div className="playlist-row" {...customAttr}>
关于如何让它发挥作用的任何想法?
答案 0 :(得分:1)
我认为应该这样做 - 在你的render()
函数中:
render() {
let customAttr = "active"
return (
<div className={"playlist-row " + customAttr}>
)
}
或者,您可以避免字符串连接...
<div className={`playlist-row ${customAttr}`}>