Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollToEnd in React Native Scrollview

I'm working on an App with React Native (expo) which has to scroll to the end of a ScrollView component. I've tried both scrollTo() and scrollToEnd() but neither is working. The error messages are always some kind of "... is not a function".

I've tried every example of the task I've found but it's still not working.

I tried different ways of using ref's and functions. For example one of the most shown patterns on the web:

const scrollRef = useRef()
<ScrollView ref={scrollRef} onContentSizeChange={scrollRef.current.scrollToEnd()}>

The error message for this one is:

TypeError: undefined is not an object (evaluating 'scrollRef.current.scrollToEnd')

(useRef() is imported from react)

I've also tried to scroll to the end via scrollTo() from a function outside of the return section but this always led to

scrollTo() is not defined

(same thing when I tried scrollToEnd())

I'm using a function and not a class in my App.js, is this making any difference in this specific case?

Please feel free to share your thoughts even if they might not be that specific, everything might be helpful.

Have a nice day.

like image 316
jan.grp Avatar asked Oct 15 '25 15:10

jan.grp


1 Answers

Change

onContentSizeChange={scrollRef.current.scrollToEnd()}

to

onContentSizeChange={() => scrollRef.current.scrollToEnd()}

Learn More about Arrow Function in Render here.

like image 154
Kartikey Avatar answered Oct 18 '25 11:10

Kartikey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!