Skip to content
Advertisement

Calling array object in src does not work

I have an array :

JavaScript

I am looping in it:

JavaScript

This here does not work: src={icon}

Correct way should be like this:

JavaScript

If I replace the {icon} with string it works! Can someone tell me why? Thank you

Advertisement

Answer

The reason this doesn’t work is because <Icon src="xxx" /> does not expect a string but an object. If you import the icons from the library and log them you will see they follow the format:

JavaScript

So what you need to do is either import the icons and use those in your element array as another answer already mentions. Or alternatively, import all icons in your application and do something like

JavaScript

but then you lose tree shaking and make your bundle a lot bigger.

Advertisement