admin管理员组文章数量:1401673
I have a function that takes a few booleans and should render an html element for each boolean if it's true. This is what I want to do:
function renderThis (first, second, third) {
if (!(first || second || third)) {
return <span>each input is false</span>
}
return (
//README: This part below doesn't work. How can I conditionally render these span tags below?
<React.Fragment>
{
first
? <span>First is true</span>
: null
second
? <span>Second is true</span>
: null
third
? <span>Third is true</span>
: null
credit
}
</React.Fragment>
)
}
The fragment doesn't work now. I can conditionally render one element (for instance, I can render first
if I remove the subsequent second
and third
variables and their associated code), but not all three. How can I conditionally render all three variables here?
I have a function that takes a few booleans and should render an html element for each boolean if it's true. This is what I want to do:
function renderThis (first, second, third) {
if (!(first || second || third)) {
return <span>each input is false</span>
}
return (
//README: This part below doesn't work. How can I conditionally render these span tags below?
<React.Fragment>
{
first
? <span>First is true</span>
: null
second
? <span>Second is true</span>
: null
third
? <span>Third is true</span>
: null
credit
}
</React.Fragment>
)
}
The fragment doesn't work now. I can conditionally render one element (for instance, I can render first
if I remove the subsequent second
and third
variables and their associated code), but not all three. How can I conditionally render all three variables here?
-
1
You could put them in an array, but why not just enclose each in
{}
? – Dave Newton Commented Dec 9, 2019 at 17:07 - Anyways why would you do that? It's a highly hardcoded and inflexible code – kind user Commented Dec 9, 2019 at 17:19
3 Answers
Reset to default 4Turn your returned elements into an array
. You can't return adjacent jsx
<React.Fragment>
{
[
true ? <span key='1'>First is true</span>: null,
true ? <span key='2'>Second is true</span>: null,
false ? <span key='3'>Third is true</span>: null
]
}
</React.Fragment>
If you are returning just the array you don't need Fragment
return [
true ? <span>First is true</span>: null,
true ? <span>Second is true</span>: null,
false ? <span>Third is true</span>: null
]
Also, you can't return adjacent jsx
from the same statement, but you can return adjacent unary statements
return(
<>
{true ? <span /> : null}
{false ? <span /> : null}
</>
)
If you want to render all three spans based on condition of each one you could do something like this:
function renderThis(first, second, third) {
if (!(first || second || third)) {
return <span>each input is false</span>;
}
return (
//README: This part below doesn't work. How can I conditionally render these span tags below?
<React.Fragment>
{first && <span>First is true</span>}
{second && <span>Second is true</span>}
{third && <span>Third is true</span>}
</React.Fragment>
);
}
The Array
approach seems overly plicated to me. Why not just this?
<React.Fragment>
{true && <span>First is true</span>}
{true && <span>Second is true</span>}
{false && <span>Third is true</span>}
</React.Fragment>
本文标签: javascriptConditionally render multiple elements inside a react fragmentStack Overflow
版权声明:本文标题:javascript - Conditionally render multiple elements inside a react fragment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744311863a2600067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论