gpt4 book ai didi

javascript - Reactjs(功能组件)使用OR语句进行条件渲染

转载 作者:行者123 更新时间:2023-12-02 18:20:41 25 4
gpt4 key购买 nike

const App = () =>{
const condition1 = true
const condition2 = false
return (
<div>
{condition1 || condition2 && <h1>condition met</h1>}
</div>
)
}

上面的代码只关心条件2,条件1被忽略。有没有办法在我的 jsx 中使用 OR 运算符?我也尝试过查看这个https://reactjs.org/docs/conditional-rendering.html我找不到我要找的东西。

最佳答案

也许你忘了加括号?

const App = () =>{
const condition1 = true
const condition2 = false
return (
<div>
{(condition1 || condition2) && <h1>condition met</h1>}
</div>
)
}

关于javascript - Reactjs(功能组件)使用OR语句进行条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70922728/

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