Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixed operators in JSX

Tags:

reactjs

jsx

I'd like to use mixed operators in JSX, for example:

{datas && datas.map(function(data,i){ return <MyComponent key={i} />}) || []}

While this technically works, ES lint warns of 'no-mixed-operators'. Is this a safe pattern to use in JSX?

like image 844
huge-iguana Avatar asked Jan 27 '17 17:01

huge-iguana


1 Answers

From the ESLint documentation:

Disallow mixes of different operators (no-mixed-operators)

Enclosing complex expressions by parentheses clarifies the developer’s intention, which makes the code more readable. This rule warns when different operators are used consecutively without parentheses in an expression.

var foo = a && b || c || d;    /*BAD: Unexpected mix of '&&' and '||'.*/
var foo = (a && b) || c || d;  /*GOOD*/
var foo = a && (b || c || d);  /*GOOD*/

If you don’t want to be notified about mixed operators, then it’s safe to disable this rule.

Hope this helps.

like image 97
Michal Cumpl Avatar answered Sep 28 '22 01:09

Michal Cumpl