question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practice regarding conditional JSX

See original GitHub issue

There seems to be several schools regarding the way to write conditional JSX. React’s documentation suggests ternary expressions or functions, there are packages using using conditional tags, such as jsx-control-statement.

I settled with ternary when there’s not too much logic or when the components are not too big (I then fallback to functions), but ternary expressions can easily get hard to read, and the syntax/spacing really differs, a lot. You can find things without any () around elements, or with weird indentation, to things like that, which readable, feel a bit verbose:

 this.props.condition === 'blah' ? (
    <span>IfBlock</span>
  ) : (
    <span>ElseBlock</span>
  )

Do you folks have an opinion on that?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:31

github_iconTop GitHub Comments

24reactions
ljharbcommented, Dec 23, 2015

Our current usage wavers between conditionally defining a variable above the jsx block (without any multiline ternaries whatsoever), or one of the following:

<div>
  {condition ? (<span />) : null}
  {condition ? (
    <div>
    </div>
  ) : null}
</div>
  {condition && (<span />)}
  {condition && (
    <div>
    </div>
  )}
</div>

The latter example looks cleanest, but it suffers from a footgun: if condition is zero, it will be toString’ed and rendered.

What would be ideal is if JSX had an operator like a ?? b which desugared to a == null ? a : b, but lacking that, and considering the footgun, I’m not sure what we should be recommending for JSX.

11reactions
otech47commented, Nov 2, 2018

Well I tried googling “eslint indent multiline ternary react” and found nothing but madness. The indent rule for ESLint could use a couple more options but I want to throw out my preferred syntax out there to see if I’m the only crazy person who renders components this way:

<Foo/>
<Bar/>
{condition == true
    ?
        <FooBar
            prop={prop}
        />
    :
        <BarFoo
            prop={prop}
        />
}
<Baz/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Ultimate Guide to Conditional Rendering in React
Conditional statements are used to execute various actions on various conditions, thus giving us the freedom to manage data in our application. In...
Read more >
Six methods to achieve conditional rendering in React - Flexiple
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if, and let React update the UI...
Read more >
React Conditional Rendering (If Else) Best Practices with 7 ...
The best thing about using an Enum object for conditional rendering is that you can make it reusable. Back to the example case,...
Read more >
Best practices for react conditional rendering? - Stack Overflow
You can read this article for more details about React conditional rendering best practices with 7 different methods.
Read more >
React conditional rendering: 9 methods with examples
In this tutorial, we'll cover the most popular ways to implement conditional rendering in React, also reviewing some tips and best practices ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found