gpt4 book ai didi

javascript - this.state.display &&

Displayed!

有效,但反之则不然?
转载 作者:行者123 更新时间:2023-12-02 23:26:16 28 4
gpt4 key购买 nike

假设我只想在 this.state.display 为 true 时显示 h1 元素。

{this.state.display && <h1>Displayed!</h1> }

有效,但是

{<h1>Displayed!</h1> && this.state.display}

没有。我在这里测试一下: https://learn.freecodecamp.org/front-end-libraries/react/use-a-ternary-expression-for-conditional-rendering

有谁知道原因吗?谢谢

最佳答案

&&意思是“如果左侧为真,则返回右侧。否则(如果左侧为假),则返回左侧。”自 <h1>Displayed!</h1>是真的,无条件评估为 this.state.display ,这不是您想要渲染的内容。

关于javascript - this.state.display && <h1>Displayed!</h1> 有效,但反之则不然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56721067/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com