简体   繁体   中英

How do I prevent a `@typescript-eslint/unbound-method` error when using `useFormikContent()`?

I have just updated some @typescript-eslint modules to the latest versions:

"@typescript-eslint/eslint-plugin": "3.4.0",
"@typescript-eslint/parser": "3.4.0",

and am now getting the following error

  22:9  error  Avoid referencing unbound methods which may cause unintentional scoping of `this`  @typescript-eslint/unbound-method

for the code

const { setFieldTouched } = useFormikContext();

which is in the same form as in the Formik docs

How do I get around this error?

Actually setFieldTouched does not use 'this' reference, so you can just disable error:

// eslint-disable-next-line @typescript-eslint/unbound-method
const { setFieldTouched } = useFormikContext();

Second option is to call setFieldTouched like object method:

  const formikContext = useFormikContext();
  const setFieldTouched = (field: string, isTouched?: boolean, shouldValidate?: boolean) =>
formikContext.setFieldTouched(field, isTouched, shouldValidate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM