Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Lifecycle callbacks are special methods in Web Components that automatically run when certain events happen to your custom element. They allow you to initialize the element, clean up resources, react to changes, and handle document movements.
Called when element is added to the page
Called when element is removed from the page
Called when observed attributes change
Called when element moves to new document
Element is created (don't do DOM work here)
Element added to page (render, fetch data, add listeners)
Attributes change (re-render, update state)
Element removed (cleanup, remove listeners, cancel timers)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lifecycle Callbacks</title>
</head>
<body>
<div class="container">
<h1>🔄 Lifecycle Callbacks</h1>
<div class="controls">
<button class="add-btn" onclick="addElement()">
➕ Add Element (connected)
</button>
<button class="remove-btn" onclick="removeElement()">
➖ Remove Element (disconnected)
</button>
</div>
<div id="element-container"></div>
<div id="log"></div>
</div>
</body>
</html>Loading preview...
connectedCallback is where you do most initialization workdisconnectedCallback is for cleanup to prevent memory leaks<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attribute Observer</title>
</head>
<body>
<div class="container">
<h1>👀 Attribute Change Observer</h1>
<div class="controls">
<h2>Update Component Attributes</h2>
<div class="control-group">
<label>Theme</label>
<select id="theme-select" onchange="updateTheme(this.value)">
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="purple">Purple</option>
<option value="orange">Orange</option>
</select>
</div>
<div class="control-group">
<label>Size</label>
<select id="size-select" onchange="updateSize(this.value)">
<option value="small">Small</option>
<option value="medium" selected>Medium</option>
<option value="large">Large</option>
</select>
</div>
<div class="control-group">
<label>Title</label>
<input type="text" id="title-input" value="Hello World"
oninput="updateTitle(this.value)" placeholder="Enter title">
</div>
<p style="color: #6b7280; font-size: 14px; margin-top: 16px;">
💡 Watch the console below to see attributeChangedCallback firing!
</p>
</div>
<div id="demo-container">
<reactive-card theme="blue" size="medium" title="Hello World"></reactive-card>
</div>
<div style="background: #1e293b; color: #e2e8f0; padding: 20px; border-radius: 16px; max-height: 200px; overflow-y: auto; font-family: monospace; font-size: 13px;" id="console" class="console-log"></div>
</div>
</body>
</html>Loading preview...
observedAttributes static propertyattributeChangedCallback fires on changes<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adopted Callback</title>
</head>
<body>
<div class="container">
<h1>📄 adoptedCallback Demo</h1>
<div class="demo-area">
<h2>🔄 Move Element Between Documents</h2>
<div class="containers">
<div class="container-box" id="doc1-container">
<span class="container-label">📄 Document 1</span>
</div>
<div class="container-box" id="doc2-container">
<span class="container-label">📄 Document 2 (New)</span>
</div>
</div>
<div class="controls">
<button onclick="createAndMove()">🎯 Create Element & Move Between Docs</button>
<button onclick="resetDemo()">🔄 Reset Demo</button>
</div>
<p style="color: #6b7280; font-size: 14px; text-align: center; margin-top: 12px;">
💡 Click the button to see adoptedCallback in action!
</p>
</div>
<div id="log">
<h3>📊 Lifecycle Events Log</h3>
<div class="log-content" id="log-content"></div>
</div>
</div>
</body>
</html>Loading preview...
adoptedCallback has the same support as other callbacks but is less commonly used.