0

Below is the code

  require("index/components/" + name); //fails
  require("index/components/myComponent"); //work fine

any good solution ?

Amit Soni
  • 3,216
  • 6
  • 31
  • 50
Aurangzaib Rana
  • 4,028
  • 1
  • 14
  • 23

2 Answers2

1

Dynamic paths in require are not currently supported. Please check this answer

This is covered in the documentation under the section "Static Resources":

The only allowed way to refer to an image in the bundle is to literally write require('name-of-the-asset') in the source.

You can use a switch statement to implement this.

Community
  • 1
  • 1
Nishant Nair
  • 1,837
  • 13
  • 19
0

I'm not sure about it but you can try to write it in ES6

require(`index/components/${name}`);

tinmarfrutos
  • 1,740
  • 1
  • 14
  • 23