I want to create a scrollable selection of images using react native. After grabbing all the needed info regarding some images I'm processing, I stack the images next to each other horizontally under a ScrollView. The issue is that it's not scrolling. Any help would be very appreciated!
Notes: Styles.borderTop only rounds the top left and right borders radius.
const ImageHeader = ({ place }) => {
if (place.image_url) {
const { Layout } = useTheme();
const [height, setHeight] = useState(0);
const [width, setWidth] = useState(0);
const [counter, setCounter] = useState(0);
const [oHeight, setOHeight] = useState(0);
const [scWidth, setScWidth] = useState(0);
const [images, setImages] = useState([]);
useEffect(()=>{
setScWidth(Dimensions.get('window').width - 2 * 8);
image_pack = place.photos || [place.image_url];
setImages(image_pack);
image_pack.forEach((img)=>{
Image.getSize(img, (iwidth, iheight) => {
setHeight(height + iheight);
setWidth(width + iwidth);
setCounter(counter + 1);
});
})
},[])
useEffect(()=> {
setOHeight(scWidth * (height/counter) / (width/counter));
console.log(`Height: ${height}, Width: ${width}, Coutner: ${counter}, OHeight: ${oHeight}, ScWidth: ${scWidth}`)
}, [counter])
return (
<View style={[{
height: oHeight || 0,
maxHeight: 450,
flex: 1
}]}>
<ScrollView horizontal showsHorizontalScrollIndicator={true} contentContainerStyle={{width}}>
{
images.map((uri, idx)=> (
<View key={idx} style={[styles.borderTop, {
width: scWidth,
height: '100%',
overflow: 'hidden'
}]}>
<Image resizeMode={'cover'} key={idx} style={[Layout.fill]} source={{uri}} />
</View>
))
}
</ScrollView>
</View>
)
}
}
By adding
contentContainerStylewidthyou are restricting the width of the whole list. You dont need that, try adding it tominWidthAnd also add
flexDirection: "row"to thecontentContainerStyleso it can stack the itemsBy reference, I have created a component that will do the work for you, as you can take a look or simple copy and use.
as you can see below I am using
recyclerlistviewORScrollView.Simple remove
recyclerlistviewif you dont want to use or simple install it