简体   繁体   English

“输入”类型上不存在属性“模糊”

[英]Property 'blur' does not exist on type 'Input'

I am using the useRef hook to pass a ref property into my custom FieldInput component.我正在使用useRef挂钩将ref属性传递到我的自定义 FieldInput 组件中。 This is then used for the validation of my form.然后将其用于验证我的表单。

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

However, I get an error on fieldRef.current that Object is possibly 'undefined'.但是,我在 fieldRef.current 上收到一个错误,即Object is possibly 'undefined'. . . In order to fix that, I made these changes:为了解决这个问题,我进行了以下更改:

const fieldRef = useRef<Input>(null);
...
fieldRef.current?.blur();

However, I still get an error that Property 'blur' does not exist on type 'Input'.但是,我仍然收到一个错误,即Property 'blur' does not exist on type 'Input'. . . Here, Input is imported from native-base.在这里, Input是从 native-base 导入的。 Due to this, I get type errors/warnings when I submit the form.因此,当我提交表单时,我会收到类型错误/警告。 An unhandled error was caught from submitForm() How can I get rid of these errors? An unhandled error was caught from submitForm()我怎样才能摆脱这些错误?

The full scenario is replicated here in a codesandbox : https://snack.expo.io/@nhammad/jealous-beef-jerky-fix完整的场景在代码和盒子中复制: https : //snack.expo.io/@nhammad/jealous-beef-jerky-fix

Ok, so first of all, in your FieldInput component, you passed those event handlers to Input like this : <Input onCahnge={callback()}/> , instead of doing it like this : <Input onChange={()=>callback()}/> .好的,首先,在您的FieldInput组件中,您将这些事件处理程序传递给Input像这样: <Input onCahnge={callback()}/> ,而不是像这样: <Input onChange={()=>callback()}/> Instead of passing your callbacks, you called them and passed in their result as the event handlers.您没有传递回调,而是调用它们并将它们的结果作为事件处理程序传递。 This caused an error in your component.这导致您的组件出错。

After fixing that and getting type hints working, blur was still signaled by the editor as missing from type Input .在修复该问题并获得类型提示工作后,编辑器仍将blur信号表示为Input类型缺失。 After looking over the docs, and trying to console log the ref element, it seems like there is actually no blur method exposed.在查看文档并尝试控制台记录 ref 元素后,似乎实际上没有公开blur方法。 TextField from react-native has it exposed, but is not forwarded to native-base Input来自react-native TextField暴露了它,但没有转发到native-base Input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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