Skip to content

Instantly share code, notes, and snippets.

@Saad-Bashar
Created March 24, 2021 15:40
Show Gist options
  • Select an option

  • Save Saad-Bashar/1bff1b2c46017b5f5e8a5be32673e723 to your computer and use it in GitHub Desktop.

Select an option

Save Saad-Bashar/1bff1b2c46017b5f5e8a5be32673e723 to your computer and use it in GitHub Desktop.
/*
This is our tab buttons, when the user press on the tab buttons
we first scroll to that index, then we set the tab active
*/
<TouchableOpacity
onPress={() => {
listRef.current.scrollToIndex({
animated: true,
viewOffset: 50 * index,
index: index,
});
setActiveTab(item)
}}
style={{ padding: 6, paddingVertical: 20 }}>
<Text style={activeTab === item ? { opacity: 1, fontWeight: "bold" } : { opacity: 0.4 }}>
{item}
</Text>
</TouchableOpacity>
/*
again we track active tab, so when the active tab changes,
we scroll the carousel to the first item of that tab category
*/
React.useEffect(() => {
if(activeTab) {
ref.current.snapToItem(DATA.map(function(e) { return e.category; }).indexOf(activeTab));
}
}, [activeTab])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment