skip to Main Content

I’m using TypeScript with React Native to develop a mobile app.
To restore the scroll position of the previous screen, I created a variable and assign the useRef() to handle the scroll.
What is the type for the ref prop?
I’ll share my code below.

const sectionListRef = useRef();
...
sectionListRef.current.ScrollToLocation // <-- Property 'scrollToLocation' does not exist on type 'never';

I have no idea which type should be…
Can someone please tell me why this error happens and how I resolve this?

2

Answers


  1. This syntax maybe help you:

    For example:

    const someRef = useRef<Your Type>(Your Initial Value);
    

    You should specify type and initial value.

    Login or Signup to reply.
  2. I did find a similar solution for FlatList, perhaps try if this could help:

    const sectionListRef = useRef<SectionList>(null);
    

    When using .scrollToLocation():

    More about scrollToLocation

    sectionListRef.current?.scrollToLocation(param);
    

    I couldn’t find any offical guide for this though, but hopefully these would help as a reference.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search