在 ScrollView 反应本机上添加 useRef 的类型

Kie*_*ood 11 typescript react-native

希望创建对 ScrollView 组件的引用,如下所示:(const ref = useRef<ScrollView>(null);或类似效果)本质上我需要访问flashScrollIndicators如下方法:

<ScrollView
  ref={ref}
  onContentSizeChange={() => ref?.current?.flashScrollIndicators()}>
  {children}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

该代码确实在我的应用程序中工作,但是 ScrollView 的打字稿定义似乎不包括 flashScrollIndicators?我正在Property 'flashScrollIndicators' does not exist on type 'ScrollView'.ts(2339)查看可以看到SectionList和Flatlist的类型,因此不确定这是否是定义的错误,或者只是我没有正确使用它。

小智 14

经过一番研究,我找不到任何具有正确解决方案的官方文档或博客文章,但使用 ScrollView 本身声明引用对我有用。所以你可以这样做:

const scrollViewRef = React.useRef<ScrollView>(null);
Run Code Online (Sandbox Code Playgroud)

然后自动完成将处理剩下的事情,希望它有帮助!