短路在反应渲染 div 中不起作用
我想在变量为 true 时启动多个组件,并且使用如下:
return (
<div className="report">
{conditionalVariable &&
<ComponentA/> &&
<ComponentB/>
}
</div>
)
但是,当变量为 true 时,只有组件 A 启动,而组件 B 启动不了。这有什么问题吗?
I want to start multiple components when a variable is true and I am using as follow:
return (
<div className="report">
{conditionalVariable &&
<ComponentA/> &&
<ComponentB/>
}
</div>
)
However, when the variable is true, only component A is getting up but not component B.What is wrong with this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很惊讶你能得到任何输出。一旦你弄清楚了逻辑,这些条件组件就需要有一个父组件 - 要么带有片段,要么带有 div 或其他东西。
您可能会看到类似于此错误的内容:
I'm surprised you're getting any output. Those conditional components once you've worked out the logic, need to have a parent - either with a fragment, or a div, or something.
You would have seen something similar to this error:
你的大括号设置得很奇怪。尝试这样:
此解决方案使用
React.Fragment
将要在相同条件下显示的两个组件分组。
Your curly braces are set rather strange. Try it like this:
This solution uses the short syntax of
React.Fragment
to group the two components you want to display under the same condition.