React: Inline If with Logical && Operator
See original GitHub issuehttps://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator
The version of ESLint you are using. 5.6.0
The problem you want to solve. Passing eslint without ignore and not outputting random numbers.
If I do:
{item.url.length && (
<a href={item.url}>
{item.title}
</a>
)}
{!item.url.length && (
<span
style={this.getStyleItemText()}
onClick={this.onItemClick}
onKeyDown={this.onItemClick}
role="button"
tabIndex={0}
>
{item.title}
</span>
)}
Result: when false it outputs the length in the dom, in this case 0.
If I do:
{item.url.length && (
<a href={item.url}>
{item.title}
</a>
) || ''}
{!item.url.length && (
<span
style={this.getStyleItemText()}
onClick={this.onItemClick}
onKeyDown={this.onItemClick}
role="button"
tabIndex={0}
>
{item.title}
</span>
) || ''}
Result: error Unexpected mix of '&&' and '||' no-mixed-operators
Your take on the correct solution to problem. Somehow allow mixing of operators in Inline If Logical statements
Are you willing to submit a pull request to implement this change? Not sure if I am able and will have time 😕
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Basic Conditional Rendering In React Using the Logical ...
Since properly written HTML code evaluates to true, using inline conditionals are a great way to take advantage of displaying content on a...
Read more >Using JavaScript inline | and && logical operators together in ...
In this tutorial learn how to combine JavaScript inline || and && logical operators together in your React components.
Read more >Six methods to achieve conditional rendering in React - Flexiple
&& is a boolean operator, which essentially means “and”. For the condition to evaluate to true, both of the statements must be individually...
Read more >What are inline conditional expressions in ReactJS
Here, Logical && operator is a boolean operator which works the same in React as it works in Javascript. It takes 2 conditions...
Read more >Utilizing AND statement (two conditions) inside react ternary ...
below is a inline if with logical && operator ...
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
Glad you figured it out @OZZIE!
For anyone else who stumbles on this thread: You can use parentheses to avoid the rule flagging an issue. Additionally, it’s possible to configure the rule to indicate that
&&
and||
should not be flagged as mixed operators. See the rule documentation for more information.problem was that if
item.url.length
was0
then it’s falsy so the part after the&&
won’t execute and the{}
gets the0
so it outputs it. By using!!
it converts the zero into booleanfalse
which won’t be outputted