首页 > 解决方案 > 如何在函数组件的 ScrollView 中使用 ScrollTo 函数?

问题描述

当元素确实安装时,我需要将 ScrollView 元素设置为我需要的位置(5 幻灯片,例如)。我怎样才能在功能组件中做到这一点?在反应原生

标签: react-native

解决方案


使用 Ref 和 onContentSizeChange

const ref = React.useRef(null);

<ScrollView
    ref={ref}
    onContentSizeChange={() => {ref.current.scrollTo({x:1000,animated: false, duration:0})}}
  >

推荐阅读