基本上,我有一个真或假的状态。州名称为DisplayFirstColorPicker。如果是真的,我希望内联样式是常量'showColorPicker',如果它是假的,它应该是常量'hideColorPicker‘。
但是,当我运行内联三元运算符时,我得到一个“无法编译”的错误。
你知道出什么问题了吗?
内联代码:
状态:
const [displayFirstColorPicker, setDisplayFirstColorPicker] = useState(false)
内联代码
<div class="firstColorPicker" style={{DisplayFirstColorPicker ? {showColorPicker} : {hideColorPicker}}}>
君士会
const showColorPicker = {
display: "block",
}
const hideColorPicker = {
display: "none",
}
发布于 2021-01-08 09:29:24
你在这里做各种奇怪的事情。外面的花括号太多了,也不应该使用像{showColorPicker}
这样的对象简写表示法,它可以编译成{ showColorPicker: { display: none} }
style={DisplayFirstColorPicker ? showColorPicker : hideColorPicker}
应该可以做到这一点。
https://stackoverflow.com/questions/65622217
复制相似问题