Skip to content

Instantly share code, notes, and snippets.

@flunder
Created April 23, 2023 13:10
Show Gist options
  • Save flunder/96da2dbcf2f76a729e70a192106ffaeb to your computer and use it in GitHub Desktop.
Save flunder/96da2dbcf2f76a729e70a192106ffaeb to your computer and use it in GitHub Desktop.
React-Native, flatlist onViewableItemsChanged get Index of page
// Quicker bi-directional onViewableItemsChanged
const [activeIndex, setActiveIndex] = useState(0);
const onViewableItemsChanged = useRef(({ viewableItems, changed }) => {
if (viewableItems?.length >= 2 && changed?.[0]?.isViewable) {
setActiveIndex(changed?.[0].index);
} else {
if (viewableItems?.[0]) setActiveIndex(viewableItems[0].index);
}
}).current;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment