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

2

1 Answer 1

2

You try like this

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

then

<Image source={item.name}></Image>
Sign up to request clarification or add additional context in comments.

1 Comment

Also keep in mind that the convention in RN is to use self-closing tags for images, like <Image ... />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.