簡體   English   中英

你如何在 React Native 中為 useRef 鈎子設置 Typescript 類型?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM