I'm doing a project in react native and my ImageBackgroud component does not want to render. The odd thing is I am already using ImageBackground in another component and it works there. I tried resizing the image but that didn't help.
Here is my component that renders child component with ImageBackground:
const DuringStay = () => {
return (
<View style={styles.container}>
<FlatList
data={test}
numColumns={2}
columnWrapperStyle={{
justifyContent: 'space-between',
marginBottom: 15,
}}
keyExtractor={item => item.id}
renderItem={({ item }) => (
<DuringStayTile item={item} />
)}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
paddingHorizontal: 20,
flex: 1,
},
});
export default DuringStay;
Here is my component that doesn't want to render ImageBackground. My data comes correctly to the component and inside Pressable i can render other components but ImageBackground won't show up
const DuringStayTile = ({item}) => {
const { title, uri } = item;
console.log(title,uri)
return (
<Pressable style={styles.container}>
<ImageBackground source={uri} resizeMode="cover" style={styles.image}>
<LinearGradient
style={styles.textBox}
colors={['transparent','rgba(0,0,0,0.6)']}>
<Text>
{title}
</Text>
</LinearGradient>
</ImageBackground>
</Pressable>
);
};
export default DuringStayTile;
const styles = StyleSheet.create({
container: {
width: '48%',
padding: 20,
borderRadius: 10,
padding:60,
backgroundColor:'pink'
},
title: {
fontWeight: 'bold',
fontSize: 16,
},
image: {
flex: 1,
overflow:'hidden',
justifyContent:'center'
},
textBox:{
position:'absolute',
right:0,
bottom:0,
width:'100%'
},
});
Data that is imported:
export const test = [
{
uri:require('../assets/f1.jpg'),
title: 'Zip Line',
id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba'
},
{
uri: require('../assets/during.jpeg'),
title: 'Blue Cave',
id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
},
{
uri: require('../assets/post.jpg'),
title: 'Rafting',
id: '58694a0f-3da1-471f-bd96-145571e29d72',
}
]
CodePudding user response:
image: {
flex: 1,
overflow:'hidden',
justifyContent:'center',
padding:30
},