0

I would like to make a dropdown which items are flags I tried a solution I found on stackoverflow but it doesn't work.

Here the sandbox link : https://codesandbox.io/embed/reverent-fermat-zssow

const CountrySelect = () => {

return (

<div className="country-select-ctn item">
     <select  name="country-select" id="country-select">
     {countries.map((option, index) =>
   <option style={{backgroundImage:`url(${option.src})`}}  key={index} value={index}> //Solution A => Empty field
     <img src={`${option.src}`} alt='foo' style={{backgroundImage:`url(${option.src})`}} />  //Solution B => [Object objet] 
   </option>
  )}
     </select>
</div>

); };

export default CountrySelect;


export let countries = [ { label:"France", src: require("./assets/flags/france.png"), link:" ", value:"FR" } ]

I want to know why am i getting [Object object]
ArBabacar_
  • 111
  • 1
  • 3
  • 11
  • 1
    Last week I was looking for some react-select examples and I found this https://codesandbox.io/s/2x16vkqkop Maybe it can help you – Drusto Oct 15 '19 at 14:09

2 Answers2

2

You're not using require. Take a look on how to import images in react

export let countries = [
    {
      label:"France",
      src: require("./assets/flags/france.png"),
      link:" ",
      value:"FR"
    }
  ]

You could also render an image instead of backgroundImage

   <option key={index} value={index}>
        <img src={option.src} alt='foo' style={{/*...*/}} />
   </option
ArBabacar_
  • 111
  • 1
  • 3
  • 11
Dupocas
  • 18,570
  • 6
  • 30
  • 49
2

Should be just:

<option style={{backgroundImage:`url(${option.src})`}}  key {index} value={index}>
Girgetto
  • 982
  • 7
  • 17