fannarsh/country-list

Changes between v1.x and v2.x?

Closed this issue · 6 comments

I am currently on version 1 and just saw that the version 2 is also available. Can you tell me if there is a breaking change when going from 1 to 2 or not?
Also what is changed in version 2?

There hasn't much changed except that you will need to change the way you import/require the lib.
We do not need to create a new instances each time we require it anymore.

--- v1.js
+++ v2.js
-var countryList = require('country-list')();
+var countryList = require('country-list');

Which also means that we can do stuff like

// v2
var { getName } = require('country-list'); 
console.log(getName('IS')); // Iceland

instead of the old way

// v1
var countryList = require('country-list')();
console.log(countryList.getName('IS')); // Iceland

And then in 2.1.0 the overwrite function was added.

const { overwrite, getName } = require('country-list');
overwrite([{
  code: 'TW',
  name: 'Taiwan'
}])

console.log(getName('TW')); // Taiwan

Hey @fannarsh thanks for writing in. I've made these changes in my code and here is my complete component:


import React, { Component } from 'react';
import { MenuItem } from 'material-ui';
import { getCodeList } from 'country-list';
import map from 'lodash/map';
import sortBy from 'lodash/sortBy';
import SelectFormField from './common/SelectFormField';

class CountrySelectFormField extends Component {
  constructor() {
    super();

    const countryOptions = map(getCodeList(), (countryName, countryCode) => ({
      value: countryCode.toUpperCase(),
      label: countryName,
    }));

    const options = sortBy(countryOptions, 'label');

    this.state = {
      options,
    };
  }

  render() {
    return (
      <SelectFormField {...this.props}>
        {this.state.options.map(option => (
          <MenuItem key={option.value} value={option.value} primaryText={option.label} />
        ))}
      </SelectFormField>
    );
  }
}

export default CountrySelectFormField;

I don't know why it has become so slow and sluggish to render the list and select the country. Also it takes forever to submit a form (calling the actual post api) with this field in it. If i remove this field, my form submission (calling the api) happens immediately.

So you are experiencing version 2 being slower then version 1?

@fannarsh yes exactly. This same code works instantly for v1.

Interesting, I'll do some benchmarking soonish.
Until that I recommend that you continue to use version 1 since that is working for you. There is no change in functionality so you don't really gain anything by upgrading.

I'm closing this since the original question has been answered.