2

Currently I am using the following code to import some module dynamically using web-packe.

I would like to know if there is a way to avoid the switch. Thanks

import React from 'react'
import iconSend from './icon-send.png'
import iconReload from './icon-reload.png'
import iconDownload from './icon-download.png'
import iconAddDoc from './icon-add-doc.png'
import iconAddAgendaItem from './icon-add-agenda-item.png'

const loadIcon = (src) => {
  switch (src) {
    case 'iconSend':
      return iconSend
    case 'iconReload':
      return iconReload
    case 'iconDownload':
      return iconDownload
    case 'iconAddDoc':
      return iconAddDoc
    case 'iconAddAgendaItem':
      return iconAddAgendaItem
  }
}

const Option = ({id, name, isActive, src}) => {
  return (
    <div>
      <div>
        <img src={loadIcon(src)} alt={name} />
      </div>
    </div>
  )
}

export default Option
simon04
  • 3,054
  • 29
  • 25
Radex
  • 7,815
  • 23
  • 54
  • 86

3 Answers3

1

I would use a Map. Something like this: Then you do not need the switch. Notice that this is react and typescript. If you are using plain react jsx instead of tsx you do not need the type Source

type Source = 'iconSend'| 'iconReload' | 'iconDownload' | 'iconAddDoc' | 'iconAddAgendaItem';
const icons: Map<Source, string> = new Map<Source, string>();

icons.set('iconSend', iconSend);
icons.set('iconReload', iconReload);
icons.set('iconDownload', iconDownload);
icons.set('iconAddDoc', iconAddDoc);
icons.set('iconAddAgendaItem', iconAddAgendaItem);

const loadIcon = (src) => {
  return icons.get(src);
};

Hope that helps!

kimy82
  • 4,069
  • 1
  • 22
  • 25
1

Possible Ways:

1- Use require and in src pass the exact name of the image, like this:

<img src={require(`./${src}`)} alt={name} />

src should be:

icon-send.png
icon-reload.png
icon-download.png
icon-add-doc.png
icon-add-agenda-item.png

2- Prepare an object with key name and image, use that to get the image directly.

Like this:

import iconSend from './icon-send.png'
import iconReload from './icon-reload.png'
import iconDownload from './icon-download.png'
import iconAddDoc from './icon-add-doc.png'
import iconAddAgendaItem from './icon-add-agenda-item.png'

let hash = {
    iconSend,
    iconReload,
    iconDownload,
    iconAddDoc,
    iconAddAgendaItem
}

<img src={ hash[src] } alt={name} />
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
0

Hi you can do it like this,

import React from 'react'
import iconSend from './icon-send.png'
import iconReload from './icon-reload.png'
import iconDownload from './icon-download.png'
import iconAddDoc from './icon-add-doc.png'
import iconAddAgendaItem from './icon-add-agenda-item.png'

const loadIcon = (src) => {
   let myDictionary = {
      iconSend: iconSend,
      iconDownload: iconDownload,
      iconAddDoc: iconAddDoc
   };
   return myDictionary[src];
}

const Option = ({id, name, isActive, src}) => {
  return (
    <div>
      <img src={loadIcon(src)} alt={name} />
    </div>
  )
}

export default Option

You can even make loadIcon as a separate file, and use it in all of your app, wherever you need it.

Adeel Imran
  • 13,166
  • 8
  • 62
  • 77