Last active
July 25, 2023 19:10
-
-
Save AnsonT/c06c5e0a131ced49477f503efff26d80 to your computer and use it in GitHub Desktop.
Autoscroll to last item in React-Native flat list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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