I currently have a simple legend component that produces an output like so:
The problem is that I manually set the height
for the blue rectangle to be as tall as the text. Is there any way I can tell that View to just expand naturally based on the height of the text instead of having to manually set some number as the height
?
You can try out the Snack or check out the code:
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View style={styles.legend}>
<View style={styles.item}>
<View style={styles.shape} />
<View>
<Text>Food</Text>
<Text>25%</Text>
</View>
</View>
<View style={styles.item}>
<View style={styles.shape} />
<View>
<Text>Utilities</Text>
<Text>35%</Text>
</View>
</View>
<View style={styles.item}>
<View style={styles.shape} />
<View>
<Text>Misc.</Text>
<Text>40%</Text>
</View>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
marginHorizontal: 50,
},
legend: {
width: '100%',
flexDirection: 'row',
justifyContent: 'space-between'
},
item: {
flexDirection: 'row',
alignItems: 'center'
},
shape: {
backgroundColor: 'blue',
width: 15,
height: 30, // I DONT WANT TO HAVE TO DO THIS
marginRight: 5
}
});
export default App;
Any help would be appreciated, thanks!