Best practice regarding conditional JSX
See original GitHub issueThere 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:
- Created 8 years ago
- Reactions:5
- Comments:31
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Our current usage wavers between conditionally defining a variable above the jsx block (without any multiline ternaries whatsoever), or one of the following:
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 toa == null ? a : b
, but lacking that, and considering the footgun, I’m not sure what we should be recommending for JSX.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: