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.

React: Inline If with Logical && Operator

See original GitHub issue

https://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. skarmklipp 2019-01-24 08 55 50

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:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
platinumazurecommented, Jan 24, 2019

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.

1reaction
OZZlEcommented, Jan 24, 2019

problem was that if item.url.length was 0 then it’s falsy so the part after the && won’t execute and the {} gets the 0 so it outputs it. By using !! it converts the zero into boolean false which won’t be outputted

Read more comments on GitHub >

github_iconTop 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 >

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