Custom element lifecycle and cleanup
Quick answer
Custom element reactions notify a component about changes such as connection to a document and disconnection. A component can use them to manage subscriptions and resources.
How it works
Use connectedCallback() for work that needs connection and disconnectedCallback() for cleanup. These callbacks may run repeatedly when an element is removed and reinserted; a move can also trigger them. Do not assume children are fully parsed in connectedCallback. Keep setup idempotent and remove external listeners with the same callback identity or an AbortController. Recreate a controller after it has been aborted. adoptedCallback is a separate reaction when a node moves to a different document; this lesson does not rely on it.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const log=[];class LessonLife extends HTMLElement{connectedCallback(){log.push("in");}disconnectedCallback(){log.push("out");}}customElements.define("lesson-life",LessonLife);const x=document.createElement("lesson-life");document.body.append(x);x.remove();document.body.append(x);console.log(log.join(","));})()Expected console output:
in,out,inReinsertion triggers connection again.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {let hits=0;class LessonWatch extends HTMLElement{connectedCallback(){this.controller=new AbortController();window.addEventListener("lesson-ping",()=>hits++,{signal:this.controller.signal});}disconnectedCallback(){this.controller.abort();}}customElements.define("lesson-watch",LessonWatch);const x=document.createElement("lesson-watch");document.body.append(x);window.dispatchEvent(new Event("lesson-ping"));x.remove();window.dispatchEvent(new Event("lesson-ping"));console.log(hits);})()Expected console output:
1The listener is removed at disconnection.
Common mistakes
Do not add another global listener each time connectedCallback runs without cleaning up. Avoid using the constructor as a substitute for connection.
Practice
Create a component that records connection and disconnection, then append, remove and append it again.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Custom element lifecycle and cleanup — Knowledge Check
Apply the concept and reason about local example output.
Can connectedCallback run again after removing and reattaching an element?
Which callback is appropriate for aborting an external subscription at removal?
Why create a fresh AbortController on a later connection?
What is the expected console output of Custom element lifecycle and cleanup example 1?
(async () => {const log=[];class LessonLife extends HTMLElement{connectedCallback(){log.push("in");}disconnectedCallback(){log.push("out");}}customElements.define("lesson-life",LessonLife);const x=document.createElement("lesson-life");document.body.append(x);x.remove();document.body.append(x);console.log(log.join(","));})()What is the expected console output of Custom element lifecycle and cleanup example 2?
(async () => {let hits=0;class LessonWatch extends HTMLElement{connectedCallback(){this.controller=new AbortController();window.addEventListener("lesson-ping",()=>hits++,{signal:this.controller.signal});}disconnectedCallback(){this.controller.abort();}}customElements.define("lesson-watch",LessonWatch);const x=document.createElement("lesson-watch");document.body.append(x);window.dispatchEvent(new Event("lesson-ping"));x.remove();window.dispatchEvent(new Event("lesson-ping"));console.log(hits);})()