I am currently following a tutorial that is about building an airbnb clone in the tutorial i tried to work with the scrollview but it doesnt work at all.
I wrote the code and tried to debug it but to no avail even copy pasted from the repository.
import React, { useRef, useState } from 'react';
import { View, Text, SafeAreaView, StyleSheet, ScrollView, TouchableOpacity } from 'react-native';
import Colors from '@/constants/Colors';
import { Ionicons, MaterialIcons } from '@expo/vector-icons';
import * as Haptics from 'expo-haptics';
import { Link } from 'expo-router';
const categories = [
{ name: 'Tiny homes', icon: 'home' },
{ name: 'Cabins', icon: 'house-siding' },
{ name: 'Trending', icon: 'local-fire-department' },
{ name: 'Play', icon: 'videogame-asset' },
{ name: 'City', icon: 'apartment' },
{ name: 'Beachfront', icon: 'beach-access' },
{ name: 'Countryside', icon: 'nature-people' },
];
interface Props {
onCategoryChanged: (category: string) => void;
}
const ExploreHeader = ({ onCategoryChanged }: Props) => {
const scrollRef = useRef<ScrollView>(null);
const itemsRef = useRef<Array<TouchableOpacity | null>>([]);
const [activeIndex, setActiveIndex] = useState(0);
const selectCategory = (index: number) => {
const selected = itemsRef.current[index];
setActiveIndex(index);
selected?.measure((x) => {
scrollRef.current?.scrollTo({ x: x - 16, y: 0, animated: true });
});
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
onCategoryChanged(categories[index].name);
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: '#fff' }}>
<View style={styles.container}>
<View style={styles.actionRow}>
<Link href={'/(modals)/booking'} asChild>
<TouchableOpacity>
<View style={styles.searchBtn}>
<Ionicons name="search" size={24} />
<View>
<Text style={{ fontFamily: 'mon-sb' }}>Where to?</Text>
<Text style={{ color: Colors.grey, fontFamily: 'mon' }}>Anywhere · Any week</Text>
</View>
</View>
</TouchableOpacity>
</Link>
<TouchableOpacity style={styles.filterBtn}>
<Ionicons name="options-outline" size={24} />
</TouchableOpacity>
</View>
<ScrollView
horizontal
ref={scrollRef}
showsHorizontalScrollIndicator={false}
contentContainerStyle={styles.scrollViewContent}>
{categories.map((item, index) => (
<TouchableOpacity
ref={(el) => (itemsRef.current[index] = el)}
key={index}
style={activeIndex === index ? styles.categoriesBtnActive : styles.categoriesBtn}
onPress={() => selectCategory(index)}>
<MaterialIcons
name={item.icon as any}
size={24}
color={activeIndex === index ? '#000' : Colors.grey}
/>
<Text style={activeIndex === index ? styles.categoryTextActive : styles.categoryText}>
{item.name}
</Text>
</TouchableOpacity>
))}
</ScrollView>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
height: 130,
elevation: 2,
shadowColor: '#000',
shadowOpacity: 0.1,
shadowRadius: 6,
shadowOffset: {
width: 1,
height: 10,
},
},
actionRow: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 24,
paddingBottom: 16,
},
searchBtn: {
backgroundColor: '#fff',
flexDirection: 'row',
gap: 10,
padding: 14,
alignItems: 'center',
width: 280,
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#c2c2c2',
borderRadius: 30,
elevation: 2,
shadowColor: '#000',
shadowOpacity: 0.12,
shadowRadius: 8,
shadowOffset: {
width: 1,
height: 1,
},
},
filterBtn: {
padding: 10,
borderWidth: 1,
borderColor: '#A2A0A2',
borderRadius: 24,
},
categoryText: {
fontSize: 14,
fontFamily: 'mon-sb',
color: Colors.grey,
},
categoryTextActive: {
fontSize: 14,
fontFamily: 'mon-sb',
color: '#000',
},
categoriesBtn: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingBottom: 8,
paddingHorizontal: 10, // Adjust padding for better spacing
},
categoriesBtnActive: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderBottomColor: '#000',
borderBottomWidth: 2,
paddingBottom: 8,
paddingHorizontal: 10, // Adjust padding for better spacing
},
scrollViewContent: {
alignItems: 'center',
paddingHorizontal:16,
},
});
export default ExploreHeader;
Heres the code but it still doesnt work as scroll view cannot be scrolled.