Issue
I am currently working on a react native component. I want to assign the height of an image to a specific value that is calulated dynamically with the screen width. I am storing the value in a variable aspectHeight
. I want to assign that value to the height in the stylesheet that I am currently calling. It is giving me the issue that the variable can not be found and I do not know how to solve this issue. I checked and I couldnt find any type of solution for this issue.
Here is my component;
import React from 'react'
import { Dimensions } from 'react-native'
import { View, Text, StyleSheet, Image } from 'react-native'
export default function PropertyTile() {
let deviceWidth = Dimensions.get('window').width - 16
var aspectHeight = (deviceWidth / 1.78) + 1
return (
<View style={styles.container}>
<View style={styles.imageContainer}>
<Image style={styles.mainImage} source={require('../../assets/luxury-home-1.jpeg')}/>
</View>
<View className='content-container'>
<Text>asdfas Tile</Text>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
height: '100%',
padding: 8
},
imageContainer: {
height: aspectHeight,
width: '100%',
backgroundColor: 'skyblue'
},
mainImage: {
width: '100%',
height: '100%'
}
})
Solution
Replace it with this;
import React from 'react'
import { View, Text, StyleSheet, Image, Dimensions } from 'react-native'
export default function PropertyTile() {
let deviceWidth = Dimensions.get('window').width - 16
var aspectHeight = (deviceWidth / 1.78) + 1
return (
<View style={styles.container}>
<View style={[styles.imageContainer,{height: aspectHeight}]}>
<Image style={styles.mainImage} source={require('../../assets/luxury-home-1.jpeg')}/>
</View>
<View className='content-container'>
<Text>asdfas Tile</Text>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
height: '100%',
padding: 8
},
imageContainer: {
width: '100%',
backgroundColor: 'skyblue'
},
mainImage: {
width: '100%',
height: '100%'
}
})
Nice day, good sir
Answered By - coolDog
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.