Created
December 24, 2020 12:05
-
-
Save AllanGraves/b720bc9a479553a3f349ec94d7704278 to your computer and use it in GitHub Desktop.
Dynamic React Native Image Loading From Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DATA = [ | |
{ | |
text: "man", | |
image: require('./assets/images/photo1.jpg') | |
}, | |
{ | |
text: "woman", | |
image: require('./assets/images/photo2.jpg') | |
} | |
] | |
const App: () => React$Node = () => { | |
const [imageVar, setImageVar] = useState(0); | |
return ( | |
<> | |
<StatusBar barStyle="dark-content" /> | |
<SafeAreaView> | |
<Image style={{height: 50, width: 50}} source={DATA[imageVar].image}/> | |
<Text> {imageVar}: {DATA[imageVar].text} </Text> | |
<Button onPress={()=> { | |
imageVar == (DATA.length - 1) ? setImageVar(0) : setImageVar(imageVar + 1); | |
console.log(DATA[imageVar].image); | |
}} | |
title="Change Pic" /> | |
</SafeAreaView> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment