Skip to content

Instantly share code, notes, and snippets.

@AnsonT
Last active July 25, 2023 19:10
Show Gist options
  • Save AnsonT/c06c5e0a131ced49477f503efff26d80 to your computer and use it in GitHub Desktop.
Save AnsonT/c06c5e0a131ced49477f503efff26d80 to your computer and use it in GitHub Desktop.
Autoscroll to last item in React-Native flat list
const AutoScrollList: FC<ListProps> = (props) => {
const listRef = useRef<FlatList>()
const [contentHeight, setContentHeight] = useState<number>()
useEffect(() => {
if (props.data?.lenngth > 0) {
listRef.current?.scrollToOffset({ offset: contentHeight })
}
}, [props.data, contentHeight])
return (
<FlatList
{...props}
ref={listRef}
onContentSizeChange={(w, h) => setContentHeight(h)}
/>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment