-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Requiring images with dynamic paths #16
Comments
Hi @amytych Yes, in my project, this code is working: import React from 'react';
export default () => (
<div className={styles.Row}>
{
[1, 2, 3].map(imageId => (
<img key={imageId} src={require(`./assets/images/dynamic/image-${imageId}.jpeg`)} />
))
}
</div>
); I now even created the same component you posted, and it is working too for me. |
This works great, how can I get this to work with query parameters (
|
I am also getting an issue importing images dynamically when appending the path with -> |
Unfortunately, I still haven't found a solution yet and I actually think this is a limitation or bug of webpack as other projects also have this problem and I couldn't find a working solution on the internet. |
And as a temporary solution (I know it is a bit verbose and not clean, I'll still try to find a solution to this issue), something like this should work in the meantime: const images = {
id1: require('./assets/images/dynamic/image-id1.png?webp'),
id2: require('./assets/images/dynamic/image-id2.png?webp'),
id3: require('./assets/images/dynamic/image-id3.png?webp'),
id4: require('./assets/images/dynamic/image-id4.png?webp'),
// ...
};
export default () => (
<div className={styles.Row}>
{
['id1', 'id2', 'id3', 'id4'].map(imageId => (
<img key={imageId} src={images[imageId]} />
))
}
</div>
); |
Hello everyone and sorry for the late reply. We can add resource queries to the first argument of import React from 'react';
const requireWebpImage = require.context('./assets/images/dynamic?webp', false, /\.png$/);
export default () => (
<div className={styles.Row}>
{
[1, 2, 3].map(imageId => (
<img key={imageId} src={requireWebpImage(`./image-${imageId}.png`)} />
))
}
</div>
); You maybe have to change the parameters of |
Ran in to this problem earlier. I was unable to use a totally dynamic path. This didn't work:
But this did work:
Also relevant if you're using dynamic paths: #141 (comment) |
In my case it was helps me
|
Thanks! This resolved my issue. |
Is it possible to require images with dynamically created paths? E.g.:
Something along the lines described in this article https://medium.com/@bogdan_plieshka/loading-static-and-dynamic-images-with-webpack-8a933e82cb1e
Thanks for any help!
The text was updated successfully, but these errors were encountered: