Based on the docs the getComponent method should be called during initialize. Becase it's a Promise the method initialize should be async.
When the component loads very quickly, this.component will be undefined in connect:
import { Controller } from '@hotwired/stimulus';
import { getComponent } from '@symfony/ux-live-component';
export default class extends Controller {
async initialize() {
this.component = await getComponent(this.element);
}
connect() {
console.log(this.component); // undefined
}
}
Adding async connect won't solve the problem. Adding a small delay will solve the problem. Another way is to call getComponent inside async connect. Either way, the documentation is a bit "obscure" about this and devs without a good JavaScript understanding will have the same issue.
I don't know if this is a bug or the way Stimulus work, but I was expecting that connect will be called after initialize resolve.
Based on the docs the
getComponentmethod should be called duringinitialize. Becase it's aPromisethe methodinitializeshould beasync.When the component loads very quickly,
this.componentwill be undefined inconnect:Adding
async connectwon't solve the problem. Adding a small delay will solve the problem. Another way is to callgetComponentinsideasync connect. Either way, the documentation is a bit "obscure" about this and devs without a good JavaScript understanding will have the same issue.I don't know if this is a bug or the way Stimulus work, but I was expecting that
connectwill be called afterinitializeresolve.