react-select2-wrapper icon indicating copy to clipboard operation
react-select2-wrapper copied to clipboard

Uncaught TypeError: Cannot read property 'current' of null

Open hakunin opened this issue 8 years ago • 11 comments

Hi, I am not sure if I am using it wrong, but this is what I get when selecting any option.

    this.$element.on('change.select2', function () {
      self.dataAdapter.current(function (data) {
        self.trigger('selection:update', {
          data: data
        });
      });
    });

screenshot 2016-10-18 16 22 29

hakunin avatar Oct 18 '16 14:10 hakunin

I found that opthers have the same erros as I do, not sure were to begin to fix it though: https://github.com/select2/select2/issues/3992

Any ideas?

hakunin avatar Nov 01 '16 14:11 hakunin

So I figured out the issue. Select2 doesn't like when its being re-rendered from react during the onChange callback.

I had to put a setTimeout there to offset the callback and now it works without throwing errors.

It might be worth doing that in the wrapper itself, or working it around as its most likely going to happen to all users.

hakunin avatar Nov 01 '16 15:11 hakunin

Can you show full example code code for reproducing that?

rkit avatar Nov 01 '16 17:11 rkit

@hakunin can you provide more info on where you put the setTimeout? I'm also getting the same error

flynfish avatar Nov 15 '16 01:11 flynfish

FYI, I never had this issue until I updated from 0.6.1 to 1.0.3, and when that happened the select2 library went from 4.0.2 to 4.0.3.

flynfish avatar Nov 15 '16 02:11 flynfish

More info:

1.0.0 is the latest version that works without getting the error. 1.0.1 didn't get there error either, but it wasn't really working correctly so I think there was a bug with that version?

flynfish avatar Nov 15 '16 02:11 flynfish

@flynfish in the onChange callback. Upon changing it, the select2 in my example is re-rendered which I is likely the reason.

hakunin avatar Nov 15 '16 06:11 hakunin

@hakunin can you provide a code snippet?

flynfish avatar Nov 17 '16 23:11 flynfish

Is there a fiddle with the wrapper linked I could fork? I don't know how to get it inside the fiddle quickly.

hakunin avatar Feb 10 '17 12:02 hakunin

The timeout workaround somehow stopped working for me. In the select2 issue there seem to be some other workaround, but the issue persists. https://github.com/select2/select2/issues/3992

hakunin avatar Feb 10 '17 13:02 hakunin

Hi,

My workaround is to use both 'select2:select' and 'change' events:

  • 'select2:select' used only when something is selected
  • 'change' used only when a previous selection is cleared (with the 'x' button)

Something like this:

var form = {};
$('select[name="'+ fieldName +'"]').select2()
.on('select2:unselect', function (event) {
    // Prevent opening dropdown when removing tags
    // see https://github.com/select2/select2/issues/3953
    if (!event.params || !event.params.originalEvent) {
        return;
    }
    event.params.originalEvent.stopPropagation();
})
.on('select2:select', function (event, par) {
    form[fieldName] = $(this).select2().val();
})
.on('change', function (event) {
    // trigger only when filters are deselected ('x' to clear)
    // when a value is selected, it triggers on 'select2:select' event
    if (!this.value) {
        form[fieldName] = [];
    }
});

zsimo avatar May 19 '17 14:05 zsimo