Search code examples
javascriptreact-nativescrollview

Add BackgroundImage to ScrollView in React-Native for JavaScript


I have this <ScrollView />

<ScrollView
        style={styles.scrollview}>
        {route.params && route.params....map((cat, index) => {
            return <Kitten
                cat={cat}
                key={index} />
        })}
    </ScrollView >

const styles = StyleSheet.create({
    scrollview: {
        paddingLeft: 15,
        paddingRight: 15,
        backgroundColor: 'white',
        flex: 1,
    },
});

into which I want to put this <BackgroundImage />.

import { ImageBackground } from 'react-native';

const image = require('...')

const Kittens = ({ children }) => {
    return <ImageBackground
        source={image}
        resizeMode='repeat'
        style={{ width: '100%', height: '100%' }}>
        {children}
    </ImageBackground>
}

I want the background image to be fixed, so that the content scrolls over the image. I further shall fill the whole screen. How would I do that?


Solution

  • use the scrollview as the child of ImageBackground

    <ImageBackground
    source={{uri://image url here}}
    style={{
    }}
    >
    <ScrollView
          style={styles.scrollview}
          contentContainerStyle={}
        >
          {route.params && route.params....map((cat, index) => {
              return <Kitten cat={cat} key={index} />
          })}
        </ScrollView >
    </ImageBackground>