Home > Enterprise >  Is there a way to know the exact type of props coming from a parent element in TypeScript?
Is there a way to know the exact type of props coming from a parent element in TypeScript?

Time:11-20

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?

CodePudding user response:

This syntax maybe help you:

For example:

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

You should specify type and initial value.

CodePudding user response:

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.

  • Related