Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upBest practice regarding conditional JSX #520
Comments
|
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 What would be ideal is if JSX had an operator like |
|
What about do ? |
|
To avoid the footgun (excellent term and I'll be using it from now on, thank you), wrap the condition in |
|
I believe our eslint config requires |
!loading ? null : (
<ActivityIndicator
size="large"
color="red"
/>
)Edit from ljharb recommendation: !!loading && <ActivityIndicator
size="large"
color="red"
/> |
|
@w0251251 that's fine! altho often people prefer to have the positive condition go first, leading to |
|
Thanks ljharb, I'll be using this style from now on its much better. return (
<View>
{ !loading && <Text>Done</Text> }
<Text>Something</Text>
{
!!loading && <ActivityIndicator
style={styles.activityIndicator}
size="large"
color="red"
/>
}
</View>
) |
|
I always feel it a little bit difficult to read when logic code mix with pure HTML. What do you guys think? |
|
@benweizhu In other words, if the logic is too complex to be appropriate inline, then it belongs in a separate component. |
|
yeah, agree, just like what react official site said about conditional rendering. My theory is we need to extract the code that exposes too many details and wrap it with something make sense(like another component). |
|
@ljharb, about your comment:
I agree with this and have seen how refactoring large components with multiple alternate render methods into smaller components has made code easier to reason about, and has also uncovered bugs that were previously unknown. Other resources that speak to the (in)correctness of
The reason for my comment here is that I'm working with a collaborator that feels that it should be ok to use multiple render methods within a component. Some questions for you:
Sorry for all the questions, and don't feel compelled to respond as I know you are busy, but I appreciate any insight you can provide! |
|
@njgraf512 that's not in any way a justification of their use; it's saying that if you do use them, their ordering is specified. The official position of this guide is DO NOT use renderFoo methods. There are still legacy places inside Airbnb where we use them, but whenever the opportunity presents itself, we strive to refactor to SFCs. |
|
@ljharb, thanks, appreciate the quick reply! |
|
What about using the Conditional Component for JSX / React? |
|
@BrodaNoel that's just reinventing JavaScript conditionals in XML, which doesn't seem like it's a cleaner approach. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Hey i made a component for conditional render is very simple i share with you
but i have a cuestion why is still evaluating the expresion that does not exist because is null and return an error like this
|
|
Well I tried googling "eslint indent multiline ternary react" and found nothing but madness. The
|
|
@otech47 the same for me, still suffering from the eslint formatting and found no wrokarounds yet |
|
Hopping into this old issue after spending time going through this repo's commit history to defend myself in a PR/check if I'm crazy I swear I had seen in this styleguide that the style of rendering jsx with multi-line ternaries is to be written as the initial comment in this issue lays it out: {condition ? (
<block />
) : (
<otherBlock />
)}So for years I've been writing my ternaries like that. A year ago when I started at a new company, I started seeing two new things. One was ternaries like this: condition
? ifTrue
: ifFalse;Aghast, I returned to my bible, which is of course this styleguide doc, and discovered the The second thing I saw for the first time here is the use of ternaries for conditional rendering when there's no second option: {condition && (
<block />
)}I again scoured these styleguides trying to figure out what was going and if it was oKaY tO uSe. All I could find was a section in the JS guide explicitly saying NOT to use selection operations in place of control statements. So I took that to mean my coworkers were clever but wrong and I continued to use ternaries with Fast forward to today, on a newer team and facing the question "shouldn't the question mark be on the next line?" on a pr and wanting to support my refutation with definitive sources, I returned again to the style guides. This time I couldn't find a single ternary in the whole JSX guide. What I found instead was a new example that included the use of |
|
@apennell both of the first two are fine; the |
|
I think that would be worth mentioning, since right now the jsx/react guide includes this as an example: // good
{showButton && <Button />} |
|
Vs
On the conditional renderer component return the children if condition is true Any advantages of doing 1 vs 2 ? |
|
The second one seems like a lot of unnecessary indirection and complexity over using simple JS syntax. |
@ljharb I heard from a colleague there is little performance benefit in using it as in 2 as the number of times render is called is less than 1? |
|
@sandeepreddy19 |
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:Do you folks have an opinion on that?