admin管理员组文章数量:1327750
I can a react ponent which is based on the const.
const MyComp = (prop) => ... etc
In it I have some JSX:
<div id="myDiv">
Stuff Here
</div>
I have a prop which is currently true called myprop.
So I want to add a condition the myDiv ... for example:
<div id="myDiv" {if myprop === true}>
Stuff Here
</div>
So that the div will show only if the prop is true.
How do I do this in React?
I can a react ponent which is based on the const.
const MyComp = (prop) => ... etc
In it I have some JSX:
<div id="myDiv">
Stuff Here
</div>
I have a prop which is currently true called myprop.
So I want to add a condition the myDiv ... for example:
<div id="myDiv" {if myprop === true}>
Stuff Here
</div>
So that the div will show only if the prop is true.
How do I do this in React?
Share Improve this question edited Nov 8, 2018 at 20:35 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Sep 10, 2018 at 9:48 user10128152user101281522 Answers
Reset to default 5If I understand your question correctly, then this should be achievable with the following JSX syntax:
{ (myprop === true) && <div id="myDiv">
Stuff Here
</div>
}
To summarize what is happening, this is effectively an expression that says, "if myprop === true" then "render the div"
As @DacreDenny said but I can short it further like
{myprop && <div id="myDiv">Stuff Here</div>}
本文标签: javascriptReact inline if condition then display this divStack Overflow
版权声明:本文标题:javascript - React: inline if condition then display this div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742231562a2437310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论