nas
nas

Reputation: 2417

How can I display the image stored in a array

I need to show a flag icon along with the text. I have all the image stored in assets folder. These are the codes that I have tried so far. I have stored the icon in a const and which I later think of calling. I want to show icon and lang horizontally with icon at left side.

const language = [
    { lang: "English", code: "en", icon: require(`../assets/us.png`) },
    { lang: "French", code: "fr", icon: require(`../assets/th.png`) },
    { lang: "Japanese", code: "jp", icon: require(`../assets/jp.png`) },
]

class App extends Component {
    onSelectLanguage = () => {
        return (
            language.map((data, i) => {
                return (
                    <View key={i} style={styles.dropDownView}>
                        <TouchableOpacity onPress={() => this.onSelectedLang(data)}>
                            <Text style={styles.dropDownText}><Image source="{data.icon}" />{data.lang}</Text>
                        </TouchableOpacity>
                    </View>
                )
            })
        )
    }
}

Upvotes: 0

Views: 48

Answers (1)

Anil Kumar
Anil Kumar

Reputation: 2309

Try this code:

const language = [
{ lang: "English", code: "en", icon: require(`../assets/us.png`) },
{ lang: "French", code: "fr", icon: require(`../assets/th.png`) },
{ lang: "Japanese", code: "jp", icon: require(`../assets/jp.png`) },

]

class App extends Component {
onSelectLanguage = () => {
    return (
        language.map((data, i) => {
            return (
                <View key={i} style={styles.dropDownView}>
                    <TouchableOpacity onPress={() => this.onSelectedLang(data)}>
                        <Text style={styles.dropDownText}><Image source={data.icon} />{data.lang}</Text>
                    </TouchableOpacity>
                </View>
            )
        })
    )
}}

Upvotes: 1

Related Questions