0

Below is the code

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

any good solution ?

3
  • what is name?... Commented May 14, 2019 at 12:52
  • where are you assigning value to name variable and which values? can you add whole code? Commented May 14, 2019 at 12:54
  • ` const path = ../../../assets/Static_Icons/${name}.${fileType};` ` iconSrc = require(path);` Commented May 14, 2019 at 13:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.