1

I wanted to be able to scroll to an element when I press on a element. (ex : I press D and it scroll down to the letter D) . Howether while there is no error here, it is not scrolling down. Here is my code simplified:

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

...

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

...

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

I used these to try to correct my code: How to add "refs" dynamically with react hooks? as well as How to scroll to an element? as well as TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339

1 Answer 1

0

To scroll a certain amount down:

fieldRef.current!.scroll(0, fieldRef.current!.scrollTop +30)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.