Does anyone have an example of how to implement the scrollToFocusedInput in a functional component with hooks (no classes). Thanks
@jjg77 I want to scroll to top, but not working for me. Have you had any luck?
Yes, I did something like this. You place this function with your flatlist and pass it as a prop to the items.
const _scrollToIndex = index => {
//console.log('called _scrollToIndex: ', index);
if (index <= comments.length && index >= 0){
flatListRef.current.scrollToIndex({animated: true, index: index})
}else{
console.log('index out of range, scroll to bottom');
flatListRef.current.scrollToIndex({animated: true, index: comments.length-1})
}
}
I am using react hooks and I am assigning ref like this.
let listRef = useRef();
const _scrollToTop = () => {
listRef.current.scrollToIndex({animated: true, index: 0})
}
<KeyboardAwareFlatList
innerRef={r => (listRef = r)}
/>
I have also tried
listRef.props.scrollToPosition(0, 0)
Both of these don't work and listRef.current throws undefined error.
@jjg77 how are assigning the ref? I'm getting null or undefined ref value.
const _scrollToTop = () => {
listRef?.current?.scrollToPosition(0,0)
}
Most helpful comment
I am using react hooks and I am assigning ref like this.
I have also tried
listRef.props.scrollToPosition(0, 0)Both of these don't work and listRef.current throws undefined error.