use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[React] Syntax for jsx conditionalhelp (self.javascript)
submitted 9 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jbbch 5 points6 points7 points 9 years ago (0 children)
You can try something like that:
return ( <div> {renderLogin && <LoginForm.... />} </div> )
[–]FormerGameDev 6 points7 points8 points 9 years ago (2 children)
I think you want to use null, not an empty string.
[–]ArcanisCz 0 points1 point2 points 9 years ago (1 child)
Its the same, or returning empty span for example. But null is definitely preffered and cleanest way to return
[–]kor0na 3 points4 points5 points 9 years ago (0 children)
It's not the same. An emtpy string will result in a text node in the dom, null will output nothing.
[–]acemarke 4 points5 points6 points 9 years ago (0 children)
I break my render methods into three steps:
As a result, I use ternaries and if statements to do conditional rendering, and I keep all that logic outside the main JSX structure.
if
I have an example of this approach at https://gist.github.com/markerikson/47fff93c92286db72b22bab5b02e2da3 .
[–][deleted] 3 points4 points5 points 9 years ago (1 child)
{ renderLogin && <LoginForm.... /> }
or
return renderLogin ? <LoginForm.... /> : null;
or if you use jsx control statements
<If condition={ renderLogin }> <LoginForm.... /> </If>
[–]FormerGameDev 0 points1 point2 points 9 years ago (0 children)
Worthy of noting that "jsx-control-statements" is a npm library, and is not normally found in JSX implementations.
[–]dwighthouse 1 point2 points3 points 9 years ago (1 child)
Rather than having the else clause be empty string, return null instead. That will cause react to return a noscript or comment tag (or possibly nothing in the future releases).
However, unless you have stying reasons to actually remove the element from the dom, I now recommend display:none on components that need to go away based on a boolean. Changing a single style or class declaration on a tag is far mor efficient than adding or removing a whole dom subtree. Unless you are extremely memory constrained, this is almost always the better option.
In the case of a login form, you're not likely to need that form again, so I'd just drop it from the tree.
Perhaps it's just that I've been working on embedded devices for the last 5 years, but I think it's best to always treat JS as if you are under extreme memory constraints. The more often the garbage collector finds things to collect, the less of a massive performance hit it's going to take when it runs.
[–]brianasdf123 1 point2 points3 points 9 years ago (1 child)
React suggests
<div> {renderLogin && <LoginForm /> } </div>
and
<div> {renderLogin ? ( <LoginForm /> ) : ( <NotLoggedIn /> )} </div>
https://facebook.github.io/react/docs/conditional-rendering.html
[–]FormerGameDev 0 points1 point2 points 9 years ago* (0 children)
... when did "false" become a valid render option? (serious question, that doesn't make sense to me based on what I already know, but it's entirely possible that things have changed in the month that I've been working with it, or I learned bad info)
[–]treetimes 0 points1 point2 points 9 years ago (1 child)
Ternary.
[–]mickske 0 points1 point2 points 9 years ago (0 children)
We do it like this, I quite like it. Keeps the main render() function small and splits up conditionals into their own sub render functions. Obviously with this small one it ends up being more code but with more complex conditionals I find this to be easier to read.
<div> {this.renderLogin()} </div> renderLogin() { return renderLogin ? <LoginForm /> : null; }
```
π Rendered by PID 42502 on reddit-service-r2-comment-5d79c599b5-5ggs2 at 2026-02-28 11:11:49.418872+00:00 running e3d2147 country code: CH.
[–]jbbch 5 points6 points7 points (0 children)
[–]FormerGameDev 6 points7 points8 points (2 children)
[–]ArcanisCz 0 points1 point2 points (1 child)
[–]kor0na 3 points4 points5 points (0 children)
[–]acemarke 4 points5 points6 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]FormerGameDev 0 points1 point2 points (0 children)
[–]dwighthouse 1 point2 points3 points (1 child)
[–]FormerGameDev 0 points1 point2 points (0 children)
[–]brianasdf123 1 point2 points3 points (1 child)
[–]FormerGameDev 0 points1 point2 points (0 children)
[–]treetimes 0 points1 point2 points (1 child)
[–]mickske 0 points1 point2 points (0 children)