![](/img/trans.png)
[英]TypeScript NextPage function getInitialProp static method with @typescript-eslint/unbound-method
[英]How do I prevent a `@typescript-eslint/unbound-method` error when using `useFormikContent()`?
我剛剛將一些@typescript-eslint
模塊更新為最新版本:
"@typescript-eslint/eslint-plugin": "3.4.0",
"@typescript-eslint/parser": "3.4.0",
現在我收到以下錯誤
22:9 error Avoid referencing unbound methods which may cause unintentional scoping of `this` @typescript-eslint/unbound-method
對於代碼
const { setFieldTouched } = useFormikContext();
與Formik 文檔中的形式相同
如何解決此錯誤?
實際上 setFieldTouched 不使用 'this' 引用,所以你可以禁用錯誤:
// eslint-disable-next-line @typescript-eslint/unbound-method
const { setFieldTouched } = useFormikContext();
第二種選擇是像 object 方法一樣調用 setFieldTouched:
const formikContext = useFormikContext();
const setFieldTouched = (field: string, isTouched?: boolean, shouldValidate?: boolean) =>
formikContext.setFieldTouched(field, isTouched, shouldValidate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.