Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Import PNG files with built-in name in a React component

There is a list of hunderds of PNG images with names consisting only of 2 letters, example aa.png, ab.png, ac.png, etc.

There is an endpoint which returns an array of object having a property name which has 3 letters, first 2 being the same letters as from the PNG images, example:

[{ name: 'aax', ... }, { name: 'aby', ... }, { name: 'acq', ... }, ... ]

I will map through this array, for each element it will be a component that will show some info for that specific object and it should also show the images with the same first 2 chars.

Here is my implementation:

import React from 'react';

import ListItem from '@material-ui/core/ListItem';
const MyComponent = (data) => {
  const flag = `../flags/${data.name.substring(0, 2).toLowerCase()}.png`;
  return (
    <ListItem>
      <div>
        <div>Info: {data.info}</div>
        <img src={flag} alt='this is a flag' />
      </div>
    </ListItem>
  );
};

export default MyComponent;

It doesn't take any image, don't know why. All those images are situated in that flags folder.

Any ideas how to solve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You might have to use require. Can you use:

const flag = require(`../flags/${data.name.substring(0, 2).toLowerCase()}.png`);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!