onConnected
Runs a function after the component's markup is in its shadow root and refs are filled.
onConnected(fn: () => unknown): voidonConnected(() => {
const chart = new Chart(refs.canvas, config);
return () => chart.destroy();
});Parameters
| Parameter | Type | Meaning |
|---|---|---|
fn |
() => unknown |
Runs once, after the first connection. If it returns a function, that function runs when the element is removed |
Order on connection
- The script runs (
defineProps,useState, …). - Reactive CSS is bound.
- The markup is cloned, bound and appended to the shadow root, and
refsfill. - A delegated
ref="control"is wired up. - The parent's
useChildrenarrays refresh. onConnectedhooks run in the order they were registered.
Moving the element does not run the hooks again. It keeps its state.
Example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>onConnected demo</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@realmorg/realm@alpha/dist/realm.css">
<script type="module" src="https://cdn.jsdelivr.net/npm/@realmorg/realm@alpha/dist/realm.js"></script>
</head>
<body>
<import-element from="./auto-focus.html"></import-element>
<auto-focus></auto-focus>
</body>
</html><custom-element name="auto-focus">
<script>
onConnected(() => {
refs.field.placeholder = "Filled in by onConnected";
});
</script>
<input ref="field" aria-label="Example field">
</custom-element>