reactjs React Native typescript :使用useRef和scrollIntoView滚动

nqwrtyyt  于 2023-01-04  发布在  React
关注(0)|答案(1)|浏览(147)

我希望能够滚动到一个元素,当我按下一个元素。(例如:我按下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

wribegjk

wribegjk1#

要向下滚动一定量:

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

相关问题