2

I am trying some react native code which is as follows , I want to set the static images to an image view as follows but it doesn't load

const items = [
            { name: './images/home.png', code: '#1abc9c' }, { name: './images/home.png', code: '#2ecc71' },
            { name: './images/home.png', code: '#3498db' }, { name: './images/home.png', code: '#9b59b6' }
        ];

        return (
            <ImageBackground
                source={require('./images/marble.jpg')}
                style={styles.backgroundImage}>

                <GridView
                    itemDimension={130}
                    items={items}
                    style={styles.gridView}
                    renderItem={item => (
                        <View style={[styles.itemContainer, { backgroundColor: item.code }]}>
                            <Image source={require(item.name)}></Image>
                        </View>
                    )}
                />

            </ImageBackground>
        );

Error I get is

calls to require expect exactly 1 string literal argument, but this was found: require(item.name).

Ofcourse I am new to react native so kindly ignore my missed terminology

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • Possible duplicate of [react native use variable for image file](https://stackoverflow.com/questions/33907218/react-native-use-variable-for-image-file) – jmargolisvt Mar 06 '18 at 19:22
  • Possible duplicate of [React Native - Image Require Module using Dynamic Names](https://stackoverflow.com/questions/30854232/react-native-image-require-module-using-dynamic-names) – sleighty Mar 06 '18 at 21:01

1 Answers1

2

You try like this

const items = [{ name: require('./images/home.png'), code: '#1abc9c' },{...}]

then

<Image source={item.name}></Image>
erkan
  • 197
  • 4