question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

destroy() fires error after second mount() call

See original GitHub issue

Hi! Thanks for very useful plugin! I founded some bug in Event Binder I try to mount Glide on small screens and destroy on large screens. When, Glide was mounted after first destroy(), on second destroy() Glide fire error in console and destroy doesn’t work There is fiddle

There is my code

window.addEventListener('resize', () => {
    if( window.innerWidth < 1200 ) {
        glide.mount();
    } else {
        glide.destroy();
    }
});

Also, if I set {type: ‘carousel’} Glide creates new clones with every attempt to call destroy()

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
ar5-incommented, Apr 1, 2021

I encountered the same issue when I tried to swap the slides and in doing so I was using destroy() and mount(). What I figured is that when you use the destroy() method, the listener property is removed and is not set when you call mount() after that. So any destroy() call will throw the error.

As a workaround, I created a new Glide object instead of calling mount() on the existing one.

instead of:

// First time I setup the slider
var glide = new Glide(slider, settings);

// ...

glide.destroy();
// ... code to update slides ...
glide.mount();

I did this:

// First time I setup the slider
var glide = new Glide(slider, settings);

// ...
glide.destroy();
// ... code to update slides ...
// Setup the slider again to initialize correctly
glide = new Glide(slider, settings);
glide.mount();

I think mount() is not the opposite of destroy() and when you call destroy once, event listeners are lost. And when we call mount() again, it doesn’t re-initializes the even listeners. May be a initialize() function that we can call before mount() could help.

3reactions
nirodaonlinecommented, Sep 17, 2020

Yeah, same scenario, same issue.

It would be nice to use mount() and destroy() indefinitely as OP mentioned.

+1 for that.

EDIT: Or better yet, a way to disable per breakpoint.

EDIT 2: Workaround: add a control variable:

let glideActive = false;

$(window).on('resize', Drupal.debounce( function () {
    if ($(this).innerWidth() <= 992) {
        if (!glideActive) {
            glide = new Glide(slider, settings);
            glide.mount();
            glideActive = true;
        }
    } else if (glideActive) {
        glide.destroy();
        glideActive = false;
    }
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to destroy or block mount events in mounted() vuejs
After re-open it(from second mount), it just keep triggers all the event and I have to block it. Is there a way that...
Read more >
useEffect(fn, []) is not the new componentDidMount()
The return value of render() is used to mount new DOM. componentDidMount fires and sets state immediately (not in an async callback); The...
Read more >
The last guide to the useEffect Hook you'll ever need
Unmounting the child component leads to an error. The child component has registered an interval that invokes a function every second. However, ...
Read more >
Lifecycle hooks - Angular
The SpyDirective implements the ngOnInit() and ngOnDestroy() hooks, and uses them to watch and report when an element goes in or out of...
Read more >
A Complete Guide to Vue Lifecycle Hooks - with Vue 3 Updates
mounted() and onMounted(). Called right after the first render of the component. The element is now available allowing for direct DOM access.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found