Skip to content

Instantly share code, notes, and snippets.

@jwheat
Last active September 25, 2018 15:44
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 jwheat/c2ada0952cf54a7256dda40af9764a71 to your computer and use it in GitHub Desktop.
Save jwheat/c2ada0952cf54a7256dda40af9764a71 to your computer and use it in GitHub Desktop.
React Native rotating images (problem)
import React, { Component } from "react";
import { AppRegistry, StyleSheet, Text, View, Image } from "react-native";
import config from "../client/config.js";
import bannerimages from "../client/images/bannerads/index";
export default class BannerAds extends Component {
constructor(props) {
super(props);
this.state = {
current_index: 0,
current_image: "../client/images/bannerads/" + config.client.bannerAds[0],
banner_images: []
};
}
componentDidMount() {
setInterval(() => {
this.displayRandomBannerAd();
console.log("changed ad");
}, 4000);
}
render() {
if (config.client.bannerAds.length > 0) {
return (
<View style={styles.container}>
<Image
style={styles.bannerImage}
source={require("../client/images/bannerads/" +
bannerimages[this.state.current_index])}
resizeMode="contain"
/>
<Text>{this.state.current_image}</Text>
</View>
);
}
}
displayRandomBannerAd = () => {
let random_index = Math.floor(
Math.random() * config.client.bannerAds.length
);
let current_image =
"../client/images/bannerads/" + config.client.bannerAds[random_index];
this.setState({
current_index: random_index,
current_image: current_image
});
};
}
AppRegistry.registerComponent("DisplayAnImageWithStyle", () => BannerAds);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
height: 50,
width: "100%"
},
bannerImage: {
height: 50,
width: "100%",
marginTop: 20
}
});
export default {
client: {
bannerAds: [
["college-ave.png"],
["discover.png"],
["fatv.png"],
["ihelp.png"],
["inceptia.png"],
["pheaa.png"],
["pnc.png"],
["sallie-mae.png"]
]
},
};
const bannerimages = {
1: "college-ave.png",
2: "discover.png",
3: "fatv.png",
4: "ihelp.png",
5: "inceptia.png",
6: "pheaa.png",
7: "pnc.jpg",
8: "sallie-mae.png"
};
export default bannerimages;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment