类型错误:_fieldRef$current.blur 不是函数 [英] TypeError: _fieldRef$current.blur is not a function

查看:47
本文介绍了类型错误:_fieldRef$current.blur 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 useRef &ref 用于包含来自 Native Base 的 的自定义 FieldInput 组件.

I used useRef & ref for a custom FieldInput component that contains <Input> from Native Base.

const fieldRef = useRef();
...
    const handleSubmitForm = (
    values: FormValues,
    helpers: FormikHelpers<FormValues>,
  ) => {
 ....
    fieldRef.current.blur();
    helpers.resetForm();
  };

但是,我在 fieldRef.current 上收到一个错误,即 Object 可能是未定义的"..所以,我把它改成这样:

However, I was getting an error on fieldRef.current that Object is possibly 'undefined'.. So, I changed it like this:

const fieldRef = useRef<any>(null);

fieldRef.current?.blur();

这修复了内联错误,但在提交表单时,我仍然得到这个:

This fixed the inline error but upon submitting the form, I still get this:

Warning: An unhandled error was caught from submitForm() TypeError: _fieldRef$current.blur is not a function

我该如何解决这个问题?任何解决方法都会有所帮助.我只想删除警告.

How can I fix this? Any workaround would help. I just want to remove the warning.

codesandbox:https://snack.expo.io/@nhammad/jealous-beef-jerky-fix

推荐答案

if 条件再添加一个检查.这应该会删除警告.

Add one more check to the if condition. This should remove the warning.

if (fieldRef && fieldRef.current && fieldRef.current.blur)

这篇关于类型错误:_fieldRef$current.blur 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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