JSX中的混合运算符 [英] Mixed operators in JSX

查看:72
本文介绍了JSX中的混合运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想在JSX中使用混合运算符:

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

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

虽然这在技术上可行,但ES皮棉警告不可混帐的操作员".这是在JSX中使用的安全模式吗?

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

推荐答案

来自 ESLint文档:

用括号括起来的复杂表达式可以阐明开发人员的意图,使代码更具可读性.当在表达式中不带括号的情况下连续使用不同的运算符时,该规则会发出警告.

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.

希望这会有所帮助.

这篇关于JSX中的混合运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆