Skip to content

Commit bb96cf6

Browse files
Add keyed directive docs (#847)
1 parent 5326c28 commit bb96cf6

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

packages/lit-dev-content/site/docs/templates/directives.md

+93
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ Lit includes a number of built-in directives to help with a variety of rendering
127127
<tr>
128128
<td>
129129

130+
[`keyed`](#keyed)
131+
132+
</td>
133+
<td>Associates a renderable value with a unique key, forcing the DOM to re-render if the key changes.</td>
134+
</tr>
135+
136+
<tr>
137+
<td>
138+
130139
[`guard`](#guard)
131140

132141
</td>
@@ -1015,6 +1024,90 @@ The `cache` directive caches the generated DOM for a given expression and input
10151024

10161025
Explore `cache` more in the [playground](/playground/#sample=examples/directive-cache).
10171026

1027+
1028+
### keyed
1029+
1030+
Associates a renderable value with a unique key. When the key changes, the previous DOM is removed and disposed before rendering the next value, even if the value—such as a template—is the same.
1031+
1032+
<table>
1033+
<thead><tr><th></th><th></th></tr></thead>
1034+
<tbody>
1035+
<tr>
1036+
<td class="no-wrap-cell vcenter-cell">Import</td>
1037+
<td class="wide-cell">
1038+
1039+
```js
1040+
import {keyed} from 'lit/directives/keyed.js';
1041+
```
1042+
1043+
</td>
1044+
</tr>
1045+
<tr>
1046+
<td class="no-wrap-cell vcenter-cell">Signature</td>
1047+
<td class="wide-cell">
1048+
1049+
```ts
1050+
keyed(key: unknown, value: unknown)
1051+
```
1052+
1053+
</td>
1054+
</tr>
1055+
<tr>
1056+
<td class="no-wrap-cell vcenter-cell">Usable location</td>
1057+
<td class="wide-cell">
1058+
1059+
Any expression
1060+
1061+
</td>
1062+
</tr>
1063+
</tbody>
1064+
</table>
1065+
1066+
`keyed` is useful when you're rendering stateful elements and you need to ensure that all state of the element is cleared when some critical data changes. It essentially opts-out of Lit's default DOM reuse strategy.
1067+
1068+
`keyed` is also useful in some animation scenarios if you need to force a new element for "enter" or "exit" animations.
1069+
1070+
{% switchable-sample %}
1071+
1072+
```ts
1073+
@customElement('my-element')
1074+
class MyElement extends LitElement {
1075+
1076+
@property()
1077+
userId: string = '';
1078+
1079+
render() {
1080+
return html`
1081+
<div>
1082+
${keyed(this.userId, html`<user-card .userId=${this.userId}></user-card>`)}
1083+
</div>`;
1084+
}
1085+
}
1086+
```
1087+
1088+
```js
1089+
class MyElement extends LitElement {
1090+
static properties = {
1091+
userId: {},
1092+
};
1093+
1094+
constructor() {
1095+
super();
1096+
this.userId = '';
1097+
}
1098+
1099+
render() {
1100+
return html`
1101+
<div>
1102+
${keyed(this.userId, html`<user-card .userId=${this.userId}></user-card>`)}
1103+
</div>`;
1104+
}
1105+
}
1106+
customElements.define('my-element', MyElement);
1107+
```
1108+
1109+
{% endswitchable-sample %}
1110+
10181111
### guard
10191112

10201113
Only re-evaluates the template when one of its dependencies changes, to optimize

0 commit comments

Comments
 (0)