Fast Element lifecycle #6631
Replies: 1 comment
-
This is expected. When observable detects a change to an observed property, it first notifies the
It is expected, though I can appreciate the confusion. The behavior doesn't implement any behavior when it gets unbound to clear the property assignment.
It depends on how the template was written, but generally the element is re-used. When the View unbinds, the elements that exist in it are moved to a DocumentFragment. When / if it is rebound, those elements are re-inserted at the binding target. If a template is written where a new template is returned from every evaluation of the binding, a new View will be created for every ViewTemplate instance and there won't be re-use of the elements: const myTemplate = html`...`'
when(x => x.test, () => myTemplate);
// vs
when(x => x.test, () => html`...`);This is great feedback. I think one option here might be to open up a configuration on |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a question related to the lifecycle for Fast Element (version 2.0.0-beta.20) - I have read The element lifecycle section in docs, but it does not provide any details on method call order.
Use case
openattribute.refon this conditionally rendered element (spanRefmember attribute).Now I need to programatically position the conditionally rendered element when it is added to DOM.
My original idea was to access that element in
openChangedmethod, but at that moment, thespanRefis not set yet. This is kind of expected.My next attempt was to call
Updates.enqueue()insideopenChanged(), but even there, the element still does not exist in DOM and thespanRefis not set.❓ Is this expected?
The next idea was to annotate the
spanRefas@observable. With thatspanRefChanged()is called. There are two surprises with this:openattribute set (<my-component open></my-component>), thespanRefChanged()is called beforeconnectedCallback().openattribute changes tofalse, itspanRefis never cleared and thespanRefChanged()is not called. This is especially confusing for any React developer where arefis cleared on its component unmount.Also when the
openis changed again totruethe original DOM element is re-used.❓ Is the
spanRefnot being cleared an expected behaviour?❓ Is the DOM element being reused guaranteed?
Based on the observed behaviour, I need to call the positioning functionality from three different places:
connectedCallback()- if the web component was mounted withopenset to true,spanRefChanged()- on the first open, if the component was mounted withopenset to false,openChanged- on any subsequent open.Repro
To test the behavior, I've created a stackblitz sandbox here: https://stackblitz.com/edit/vitejs-vite-dhhejl?file=src/main.ts.
It allows to mount the component either initially open or closed and delete the component.
The lifecycle method calls are logged to the page and console.
nit: Another surprising fact was that
openChangedmethod is first called before class constructor. When I realize that is implemented using a setter/getter on theopenfield which uses a class field initializer, it makes sense, but still might confuse someone.Beta Was this translation helpful? Give feedback.
All reactions