Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Cannot read property 'current' of null #52

Open
hakunin opened this issue Oct 18, 2016 · 11 comments
Open

Uncaught TypeError: Cannot read property 'current' of null #52

hakunin opened this issue Oct 18, 2016 · 11 comments

Comments

@hakunin
Copy link

hakunin commented Oct 18, 2016

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
Copy link
Author

hakunin commented Nov 1, 2016

I found that opthers have the same erros as I do, not sure were to begin to fix it though:
select2/select2#3992

Any ideas?

@hakunin
Copy link
Author

hakunin commented Nov 1, 2016

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.

@rkit
Copy link
Owner

rkit commented Nov 1, 2016

Can you show full example code code for reproducing that?

@flynfish
Copy link

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

@flynfish
Copy link

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
Copy link

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?

@hakunin
Copy link
Author

hakunin commented Nov 15, 2016

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

@flynfish
Copy link

@hakunin can you provide a code snippet?

@hakunin
Copy link
Author

hakunin commented Feb 10, 2017

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

@hakunin
Copy link
Author

hakunin commented Feb 10, 2017

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

@zsimo
Copy link

zsimo commented May 19, 2017

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] = [];
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants