I'm trying to dynamically load an image in some React Component. I'm using require instead of simply passing the paths in order to improve performance.
I gathered I could use an inline require and it does work. However, when I try to pass as a prop instead, I get errors.
What am I doing wrong?
EDIT: Turns out both work, I was doing something else wrong which was throwing the error. (Bonus question still applies tho)
import React from 'react';
// This works
export SomeComponent = () => (
<div>
<img src={require(`../images/my-logo.svg`)} />
</div>
)
// This works too!
export SomeComponent = ({image}) => (
<div>
<img src={require(`../images/${image}`)} />
</div>
)
<SomeComponent image="my-logo.svg" />
Bonus question: Can this be done with ES6 import vs CommonJs require?
create-react-app?