I try to randomly load a gif from an array. I tried several ways to do it but none works. I either get an error message or the image just won't appear.
Version 1 (result: image doesn't appear):
var myPix = new Array("../assets/class/emojis/correct/clapping_hands.gif", "../assets/class/emojis/correct/beaming_face_with_smiling_eyes.gif","../assets/class/emojis/correct/confetti_ball.gif","../assets/class/emojis/correct/flexed_biceps.gif");
var randomNum = Math.floor(Math.random() * myPix.length);
var theImage= myPix[randomNum];
return (
<View>
<Image
style={styles.gifAnimation}
source={theImage}
/>
</View>
Version 2 (result: "invalid call")
var myPix = new Array("../assets/class/emojis/correct/clapping_hands.gif", "../assets/class/emojis/correct/beaming_face_with_smiling_eyes.gif","../assets/class/emojis/correct/confetti_ball.gif","../assets/class/emojis/correct/flexed_biceps.gif");
var randomNum = Math.floor(Math.random() * myPix.length);
var theImage= myPix[randomNum];
return (
<View>
<Image
style={styles.gifAnimation}
source={require(myPix[randomNum])}
/>
</View>
Version 3 (result: image won't load):
const [theImage, setTheImage] = useState();
React.useEffect(() => {
var myPix = new Array(
"../assets/class/emojis/correct/clapping_hands.gif",
"../assets/class/emojis/correct/beaming_face_with_smiling_eyes.gif",
"../assets/class/emojis/correct/confetti_ball.gif",
"../assets/class/emojis/correct/flexed_biceps.gif",
);
var randomNum = Math.floor(Math.random() * myPix.length);
var x = myPix[randomNum];
setTheImage_Correct(x);
source={image_correct
Any thoughts?