0

I want to randomise the return of an image in a folder but I have the error 'Invalid call'

my function:

  returnGif = () => {
    min = Math.ceil(1);
    max = Math.floor(53);
    const gifNumber = Math.floor(Math.random() * (max - min +1)) + min;
    var gifFont = require('../config/Images/Gifs/' + gifNumber + '_2.png');
    if (gifFont) {
        if (Math.random()>0.5) gifFont = require('../config/Images/Gifs/' + gifNumber + '_2.png');
        else gifFont = require('../config/Images/Gifs/' + gifNumber + '.png');
    }
    else gifFont = require('../config/Images/Gifs/' + gifNumber + '.png');
    return gifFont;
  }

1 Answers1

1

In react native ,Dynamic paths in require are not currently supported.

you need to import all images and then use case for randomization

for example

 function getavatarImage(avatar) {
  switch (avatar) {
   case "spiderman":
    return require('./spiderman.png');
   case "batman":
    return require('./batman.png');
   case "hulk":
    return require('./hulk.png');
   default:
    return require('./no-image.png');
 }
}
Aurangzaib Rana
  • 4,028
  • 1
  • 14
  • 23