Chun-Lin / react-select-country-list

This is the country list data format for react-select
MIT License
52 stars 35 forks source link

Duplicate label when using setEmpty() #10

Open mcelearr opened 2 years ago

mcelearr commented 2 years ago

When I set an initial empty value like so:

  const countries = useMemo(() => {
    return countryList().setEmpty("Select a Country").getData();
  }, []);

I end up with a duplicate empty value Screenshot 2022-01-12 at 17 39 41

Am I using the .setEmpty() method wrong?

I messed around with the setEmpty() method and saw that changing to a .concat method instead of .unshift fixed the problem for me. Not sure why. Want me to open a PR or can you think of a cleaner way to do it?

  setEmpty(label) {
    this.data = [{
      value: '',
      label: label,
    }].concat(this.data)
    this.valueMap[''] = label
    this.labelMap[label] = ''

    return this
  }