gpt4 book ai didi

reactjs - 使用 TypeScript 在 React Native 上传递 refs : (property) React. MutableRefObject.current : null Object is possibly 'null' . ts(2531)

转载 作者:行者123 更新时间:2023-12-05 08:49:36 24 4
gpt4 key购买 nike

我在 React Native 表单上有几个字段,我希望每次用户使用虚拟键盘验证字段时,焦点都从一个跳到下一个。

我想到了类似的东西:

        <NamedTextInput
name={'email'}
ref={emailRef}
...
onSubmitEditing={() => passwordRef.current.focus()}
/>

<NamedTextInput
name={'password'}
ref={passwordRef}
...
/>

目前我收到 TypeScript 错误:

(property) React.MutableRefObject<null>.current: null
Object is possibly 'null'.ts(2531)

我试着添加!和 ?标记,但它结束于:

Property 'focus' does not exist on type 'never'.ts(2339)

有解决这个问题的想法吗?

谢谢

最佳答案

好的,我找到了这个。我忘记在 useRef 声明中输入:

const passwordRef = useRef<TextInput>(null)

然后我可以添加一个!标记或检查当前引用是否存在而不会出现“从不”错误:

onSubmitEditing={() => (passwordRef.current && passwordRef.current.focus())}

关于reactjs - 使用 TypeScript 在 React Native 上传递 refs : (property) React. MutableRefObject<null>.current : null Object is possibly 'null' . ts(2531),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63652477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com