0

Below is the code

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

any good solution ?

Amit Soni
  • 3,136
  • 5
  • 28
  • 49
Aurangzaib Rana
  • 3,558
  • 1
  • 11
  • 20

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,627
  • 9
  • 19
0

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

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

tinmarfrutos
  • 1,495
  • 1
  • 10
  • 22