[英]ReactJS Why is my DOM / `display` only rendering 'display: none'?
Driving me a bit nuts here... I want to click a Preview
button and it generates a Preview Pane for text input. 在这里让我有点生气...我想单击“
Preview
按钮,它会为文本输入生成一个“预览窗格”。 I thought i'll get the div's inline-style linked to React state. 我以为我会把div的内联样式链接到React状态。
<div className="preview-container" style={{ display: this.state.showPreview ? 'visible' : 'none' }}>
{
this.state.showPreview &&
<Preview
htmlString={value.toString('html')}
showOrHidePreview={this.togglePreview}
/>
}
</div>
But even though i can see state changing when i click the button, AND i've successfully changed a border colour by linking it to state - for some reason changing display to none just sticks. 但是,即使单击按钮时我可以看到状态发生了变化, 并且我已经通过将边框链接到状态成功地更改了边框颜色-出于某种原因,将显示更改为没有显示只是停留。 it wont move off it.
它不会离开它。
Any ideas? 有任何想法吗?
Updated (fairly standard toggle handler):- 更新(相当标准的切换处理程序):-
togglePreview = () => {
if (this.state.showPreview === false) {
this.setState({
showPreview: true,
})
} else {
this.setState({
showPreview: false,
})
}
}
Thanks v.much @AdrianoRepetti for the suggestion of display: block
rather than visible
. 感谢v.much @AdrianoRepetti对
display: block
而不是visible
的建议。 Not sure where i picked up display: visible
from. 不知道我在哪里拿起
display: visible
从display: visible
。
If anyone else happens across this, i also found a hack using opacity: 10 vs opacity 0. Though that was scraping the barrel a bit. 如果发生任何其他情况,我也发现了使用不透明度的黑手:10 vs不透明度0。尽管那有点令人费解。
There are several issues with this code: 此代码存在多个问题:
display: visible
is incorrect and will have no effect (ignored by browser). display: visible
不正确,将不起作用(被浏览器忽略)。 The correct value here is display: block
. 此处的正确值是
display: block
。
You are removing the contents when showPreview
is false
, so display: none
has no effect either. 当
showPreview
为false
,您将删除内容,因此display: none
也不起作用。
When showPreview is false
you are asking React to render the value false
. 如果showPreview为
false
,则要求React将该值呈现为false
。 This is wrong, and you will get error. 这是错误的,并且您将得到错误。
The max value of opacity
is 1
. opacity
的最大值是1
。
What is the initial value of showPreview? showPreview的初始值是多少? If
undefined
, your toggle will set showPreview to false
, giving you the impression that it didn't do anything at all. 如果为
undefined
,则您的切换会将showPreview设置为false
,给您的印象是它什么都不做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.