【问题标题】:React Native Typescript : scroll with useRef and scrollIntoViewReact Native Typescript:使用 useRef 和 scrollIntoView 滚动
【发布时间】:2023-01-03 22:23:05
【问题描述】:

当我按下一个元素时,我希望能够滚动到一个元素。 (例如:我按 D 并向下滚动到字母 D)。然而,虽然这里没有错误,但它没有向下滚动。 这是我的代码简化:

const fieldRef = React.useRef<null | HTMLInputElement>(null);
  const scrollToElement = () => fieldRef.current?.scrollIntoView();
 

...

                    <Text
                      onPress={() => {
                        {
                          scrollToElement;
                        } }}
                     > 
                         Here
                     </Text>

...

<div ref={fieldRef}>
    <Text> Hello</Text>
</div>

我用这些来尝试更正我的代码: How to add "refs" dynamically with react hooks?How to scroll to an element?TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339

【问题讨论】:

    标签: reactjs typescript react-native scroll


    【解决方案1】:

    向下滚动一定数量:

    fieldRef.current!.scroll(0, fieldRef.current!.scrollTop +30)
    

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 2020-07-30
      • 2021-09-10
      • 1970-01-01
      • 2021-01-10
      • 2019-08-16
      • 2020-05-09
      • 2020-06-18
      • 2021-04-02
      相关资源
      最近更新 更多