首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用` `useFormikContent()`时,如何防止`@typescript-eslint/unbound-method` `错误?

使用` `useFormikContent()`时,如何防止`@typescript-eslint/unbound-method` `错误?
EN

Stack Overflow用户
提问于 2020-06-29 22:14:22
回答 1查看 5.8K关注 0票数 1

我刚刚将一些@typescript-eslint模块更新到了最新版本:

代码语言:javascript
复制
"@typescript-eslint/eslint-plugin": "3.4.0",
"@typescript-eslint/parser": "3.4.0",

我现在得到了以下错误

代码语言:javascript
复制
  22:9  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method

以获取代码

代码语言:javascript
复制
const { setFieldTouched } = useFormikContext();

它与Formik docs中的格式相同。

我如何避免这个错误?

EN

回答 1

Stack Overflow用户

发布于 2020-07-07 19:35:30

实际上setFieldTouched并不使用'this‘引用,所以你可以直接禁用error:

代码语言:javascript
复制
// eslint-disable-next-line @typescript-eslint/unbound-method
const { setFieldTouched } = useFormikContext();

第二种选择是调用类似setFieldTouched的对象方法:

代码语言:javascript
复制
  const formikContext = useFormikContext();
  const setFieldTouched = (field: string, isTouched?: boolean, shouldValidate?: boolean) =>
formikContext.setFieldTouched(field, isTouched, shouldValidate);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62640074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档