箭头函数不应返回赋值 no-return-assign [英] Arrow function should not return assignment no-return-assign

查看:25
本文介绍了箭头函数不应返回赋值 no-return-assign的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

然而,我的代码在应用程序中正常工作,在提交 husky 运行并给出错误提示箭头函数不应返回赋值 no-return-assign"

My code is working correctly in the app however, before commit husky runs and gives out the error saying "Arrow function should not return assignment no-return-assign"

           <Field name='dob' label='dateOfBirth'
              placeholder=''
              onFocus={(e) => e.target.placeholder = 'MM/YYYY/DD'}
            />

推荐答案

正如一些人在评论中提到的,问题在于您正在使用

As several people mention in comments, the issue is that you're using

(e) => e.target.placeholder = 'MM/YYYY/DD'

大致相当于

anon_func = function (e) {
    return e.target.placeholder = 'MM/YYYY/DD';
}

因为 (args) => 表示对表达式求值并返回结果.

because (args) => <expression> means to evaluate the expression and return the result.

与 jakemingolla 的回答相反,这 是合法的;它返回 'MM/YYYY'DD' 这在这种情况下无关紧要,因为您不关心任何返回值.这就是它有效"的原因.但它通常被认为是糟糕的风格,这就是为什么你的预提交检查会标记它.

Contrary to jakemingolla's answer, this is legal; it returns 'MM/YYYY'DD' which doesn't matter in this situation since you don't care about any return value. That's why it "works". But it is generally regarded as poor style, which is why your pre-commit checks flag it.

你想要的是(args) =>{<function-body>},如果你没有明确返回某些东西,它(就像任何直接声明的函数体一样)只会返回 undefined .那是

What you want is (args) => {<function-body>}, which (like any directly-declared function body) just returns undefined if you don't explicitly return something. That is

(e) => {e.target.placeholder = 'MM/YYYY/DD';}

大致是这样

anon_func = function (e) {
    e.target.placeholder = 'MM/YYYY/DD';
}

这就是你想要的.

这篇关于箭头函数不应返回赋值 no-return-assign的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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