7

I've been trying to render a background image, and it runs, but nothing appears. I'm running this through Android on Windows. This is the code below:

import React, { Component } from 'react';

import { View, Image, Text, ImageBackground } from 'react-native';

class Background extends Component {
    render() {
        return (
      <ImageBackground
        source={{uri: 'https://thumbs.dreamstime.com/b/purple-blue-textured-background-wallpaper-app-background-layout-dark-gradient-colors-vintage-distressed-elegant-78118630.jpg'}}
        style={{flex: 1, width: '100%'}}

      >
        <View >
          <Text>Test</Text>
        </View>
      </ImageBackground>
        );
    }
  }

export default Background;

I'm not sure if the code just isn't properly pulling the image itself or if the styling needs to be adjusted. Thanks for the help!

Eric Dulin
  • 71
  • 1
  • 1
  • 3
  • I may be wrong, but I guess you are missing ```height: '100%'```. in your ```ImageBackground``` component. Can you check it, please? – soutot May 14 '18 at 17:12

4 Answers4

15

Your ImageBackground component needs a height value in your style attribute. RN is picky about that.

vm909
  • 581
  • 3
  • 8
4
import { ImageBackground } from 'react-native'

<ImageBackground
source={require('../assets/background.jpg')}
resizeMode={'cover'}
style={{ flex: 1, width: '100%' }}></ImageBackground>
Sir Alidadi
  • 203
  • 2
  • 6
  • 1
    Code-only answers are generally considered low-quality answers on Stack Overflow. This answer could be improved with an explanation as to how this solves OP's problem presented in the question. – codewario May 31 '22 at 14:17
0

for me only shows when i use

require()

like this post ImageBackground not working when i call source from state

source={require( 'https://thumbs.dreamstime.com/b/purple-blue-textured-background-wallpaper-app-background-layout-dark-gradient-colors-vintage-distressed-elegant-78118630.jpg')}

(i tried to import image and call inside source but don't appears, even declaring in style height and width, only after use require(), why? i don't know)

reinaldo
  • 9
  • 8
-1

Wrap your ImageBackground component within a Container component.

class Background extends Component {
    render() {
        return (
      <Container>
        <ImageBackground
        source={{uri: 'https://thumbs.dreamstime.com/b/purple-blue-textured-background-wallpaper-app-background-layout-dark-gradient-colors-vintage-distressed-elegant-78118630.jpg'}}
        style={{flex: 1, width: '100%'}}>
         <View >
           <Text>Test</Text>
         </View>
       </ImageBackground>
      </Container>
        );
    }
  }
Shweta
  • 1,099
  • 1
  • 9
  • 9