[英]How do you set the Typescript type for useRef hook in React Native?
如何正确键入 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());
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.