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

Wrong lifecycle order when using idiomorph #808

Open
davidjr82 opened this issue Jan 11, 2025 · 0 comments
Open

Wrong lifecycle order when using idiomorph #808

davidjr82 opened this issue Jan 11, 2025 · 0 comments

Comments

@davidjr82
Copy link

Hi,

there is an issue when using new morph libraries like idiomorph. Idiomorph will insert first the new element in the dom and after remove the old one.

Due to this, the expected lifecycle when loading and replacing a frame (connect - disconnect - connect) is not happening. What happens is the wrong lifecycle (connect - connect - disconnect), letting the component in a wrong state (disconnected).

Let's put a simple controller with tomselect. In this example, at first the tomselect will load correctly. But as soon as the frame is replaced, tomselect is no longer working because the last method call is disconnect() as I explained before, with this output in the console log:

controller: connect
controller: connect
controller: disconnect
import { Controller } from "@hotwired/stimulus";
import TomSelect from "tom-select";

export default class extends Controller {

    static values = {
        options: Object,
    };

    connect() {
        console.log('controller: connect');

        new TomSelect(this.element, {});
    }

    disconnect() {
        console.log('controller: disconnect');

        if (this.element.tomselect) {
            this.element.tomselect.destroy();
        }
    }
}

If I put a setTimeout(() => { ... }, 100); around the connect code, then it works as expected (jumpy, but it will delay the execution of the connect logic to after the disconnect method execution):

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

No branches or pull requests

1 participant