Friday, May 17, 2024
157
rated 0 times [  158] [ 1]  / answers: 1 / hits: 17124  / 7 Years ago, wed, november 22, 2017, 12:00:00

I am completing a tutorial on react native. For a certain screen, the instructor recommends the following code:



<Image source={bgImage} style={styles.backgroundContainer}> 
<View style={styles.container}>
<Quote quoteText={this.props.text} quoteSource={this.props.source}/>
</View>
</Image>


But when I use this, I get the above error.



I've tried alternatives to this, but when I do, the background image doesn't even load.



EDIT: As requested below, here's my styling code. What I'm going for is using a background gradient image stored locally to the app code with text overlayed over that background. What I currently get by using the suggestion below is just the text at the very top of the screen and no background image.



const styles = StyleSheet.create({
backgroundContainer: {
flex: 1,
resizeMode: 'cover',
width: undefined,
height: undefined,
backgroundColor: '#889DAD',
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
},
});

More From » react-native

 Answers
9

You are not allowed to put other components inside an image component. You need to wrap a View around your Image and positioned it as absolute.



<View style={{ flex: 1}}>
<Image source={bgImage} style={styles.backgroundContainer} />
<View style={styles.container}>
<Quote quoteText={this.props.text} quoteSource={this.props.source}/>
</View>
</View>


container: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
justifyContent: 'center',
alignItems: 'center',
},


EDIT:
Since react-native version 50.0, you can simply use ImageBackground


[#55864] Monday, November 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
averyc

Total Points: 102
Total Questions: 113
Total Answers: 89

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
;