Skip to content

Instantly share code, notes, and snippets.

@AllanGraves
Created December 24, 2020 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllanGraves/b720bc9a479553a3f349ec94d7704278 to your computer and use it in GitHub Desktop.
Save AllanGraves/b720bc9a479553a3f349ec94d7704278 to your computer and use it in GitHub Desktop.
Dynamic React Native Image Loading From Array
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