question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requiring images with dynamic paths

See original GitHub issue

Is it possible to require images with dynamically created paths? E.g.:

import React from 'react';

export default ({ imgName }) => (
  <div>
    <img src={require(`./images/${imgName}`)} />
  </div>
);

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!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
james-walliscommented, Jul 1, 2020

Ran in to this problem earlier. I was unable to use a totally dynamic path.

This didn’t work:

const sauce = '../../images/background.jpg';
<img src={require(`${sauce}`)} />

But this did work:

const sauce = 'background.jpg';
<img src={require(`../../images/${sauce}`)} />

Also relevant if you’re using dynamic paths: https://github.com/cyrilwanner/next-optimized-images/issues/141#issuecomment-623556804

5reactions
kotaycommented, Jul 2, 2018

This works great, how can I get this to work with query parameters (?webp), the following does not seem to work:


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}.png?webp`)} />
      ))
    }
  </div>
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native - Image Require Module using Dynamic ...
This is exactly what I was looking for. I am creating a local "data" file in my app and I needed to access...
Read more >
Static VS Dynamic Images Paths in VUE3 | CodeX - Medium
The requirement for this part is to show the right picture in a list of team members. Different pictures need to be allocated...
Read more >
Using dynamic image paths in .vue files - Mitya.uk
A common issue when using frameworks like Nuxt or Vue CLI comes when using dynamic image paths. This is essentially due to Webpack....
Read more >
Import images dynamically using require.context - MeloSpace
After researching more, I realised that require only work with static paths (i.e. a hardcoded path instead of a path stored in a...
Read more >
next/image - Next.js
Enable Image Optimization with the built-in Image component. ... Dynamic API Routes ... The <Image /> component requires the following properties.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found