React-Select与Formik – S6544承诺不应被滥用

huangapple go评论69阅读模式
英文:

React-Select with Formik - S6544 promises should not be misused

问题

我有以下代码:

<Select 
   options={publisherOptions} 
   onChange={selectedOption => formik.setFieldValue("publisherId", selectedOption?.value)} />

它可以工作!但是我收到了 TypeScript 警告 S6544

Promises should not be misused.
Promise-returning function provided to attribute where a void return was expected.

有没有更好的方法来编写代码以避免这个警告?

英文:

I have following code

&lt;Select 
   options={publisherOptions} 
   onChange={selectedOption =&gt; formik.setFieldValue(&quot;publisherId&quot;, selectedOption?.value)} /&gt;

and it works! However I am getting typescript warning S6544

> Promises should not be misused.
Promise-returning function provided to attribute where a void return was expected.

React-Select与Formik – S6544承诺不应被滥用

Is there some better way how to code this to avoid the warning?

答案1

得分: 1

不要从函数中返回 Promise。

selectedOption => {
    formik.setFieldValue...;
}

在函数体周围添加大括号意味着它返回 return 语句所指定的内容,而不是单个表达式的结果。(由于没有 return 语句,它返回 undefined(上下文所期望的 void 值)。

英文:

Don't return the promise from the function.

selectedOption =&gt; {
    formik.setFieldValue...;
}

Adding braces around the function body means it returns whatever the return statement says instead of the result of the single expression. (Since there is no return statement, it returns undefined (a void value as is expected by the context).

huangapple
  • 本文由 发表于 2023年8月8日 22:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定