gpt4 book ai didi

reactjs - 如何在 React Native 中为 useRef 钩子(Hook)设置 Typescript 类型?

转载 作者:行者123 更新时间:2023-12-04 01:36:28 25 4
gpt4 key购买 nike

如何正确键入 React Native TextInput 的 useRef?
使用以下代码,我收到以下错误。

Property 'isFocused' does not exist on type 'MutableRefObject<TextInput>'



import React, { useRef } from 'react';
import { TextInput } from 'react-native';

const TestScreen = () => {

const searchInputRef = useRef<TextInput>();

const updateSearchText = (searchText: string) => {
console.log(searchTextRef.isFocused()); // 👈 Error here.
};

return (
<TextInput
ref={searchInputRef}
placeholder="Search"
onChangeText={(text: string) => updateSearchText(text)}
autoCorrect={false}
autoCapitalize="none"
value={searchText}
/>
)

}

最佳答案

它应该是

 const updateSearchText = (searchText: string) => {
console.log(searchInputRef.current.isFocused());
};

关于reactjs - 如何在 React Native 中为 useRef 钩子(Hook)设置 Typescript 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350522/

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