我在定义参考文献时遇到了一个问题,即.

inputRef = React.createRef(null)

//...

const someFunction () => {
 if (this.inputRef && this.inputRef.current) {
   this.inputRef.current.focus()
 }
}

//...

<TextInput ref={inputRef} />

当我访问.focus()时,会出现以下错误:

[ts]属性"focus"在类型"never"上不存在.[2339]

我能不能告诉createRef,这个ref可以是nullTextInput,所以它知道.focus()可能存在?

推荐答案

您可以try 以下方法:

inputRef = React.createRef<TextInput>();

React-native相关问答推荐

如何在renderItem中显示每个项目的索引号,保持分页的前一个下一个按钮

在原生react 中更改图像源URI时闪烁

@testing-library/react-native -> 渲染 -> TypeError:无法读取未定义的属性(读取存在)

单击react 本机时如何从同一屏幕更改内容

有人可以帮我实现 Font.loadAsync

useState 函数似乎阻止了 Animated.timing 事件?

react-native: 'adb' 不是内部或外部命令、可运行程序或批处理文件

设置 rootView.appProperties 不会重新渲染根组件

使用 Hooks 在功能组件中react-navigation 标题

修复错误:路由 'Home' 的组件必须是 React 组件

React-Native + crypto:如何在 React-Native 中生成 HMAC?

如何从 React Native 应用程序打开 Google Play store ?

当 textInput 聚焦时,第一次touch flatList 不起作用,但是第二次起作用

在 React Native App 中禁用屏幕捕获/ScreenShot

将 react-native 元素Bundle 为 iOS 框架或 (.aar) Android 库

检测是否通过推送通知打开了 React Native iOS 应用

React Native如何将this.setState更改传递给父级

React Native:如何将 的高度和宽度设置为 容器?

使用expo react-native链接?

React Native:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败