Skip to content

Commit 8858e83

Browse files
committed
Break render cycles with input, textarea, etc
Don't set props if prop value is already the same as incoming prop value.
1 parent f9c99a9 commit 8858e83

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

typescript/packages/common-html/example/main.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,26 @@ setDebug(true);
99
// setEventSanitizer(...);
1010

1111
const text = state("text", "Hello, world!");
12-
const clicks = stream("clicks");
12+
const input = stream<InputEvent>("clicks");
13+
14+
input.sink((event) => {
15+
console.log("input", event);
16+
const target = event.target as HTMLInputElement | null;
17+
const value = target?.value ?? null;
18+
if (value !== null) {
19+
text.send(value);
20+
}
21+
});
1322

1423
// Build template
1524
const renderable = html`
1625
<div class="container">
1726
<h1 class="title">${text}</h1>
18-
<button onclick=${clicks}>Click me</button>
27+
<input type="text" oninput=${input} value=${text} />
1928
</div>
2029
`;
2130

2231
// Render
2332
const dom = render(renderable);
2433

25-
clicks.sink((value) => {
26-
console.log("clicks", value);
27-
});
28-
29-
setInterval(() => {
30-
text.send(`Hello, world! ${new Date().toLocaleTimeString()}`);
31-
}, 1000);
32-
3334
document.body.appendChild(dom);

typescript/packages/common-html/src/render.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ const listen = (
112112
};
113113
};
114114

115-
const setProp = (element: HTMLElement, key: string, value: unknown) => {
115+
const setProp = <T>(target: T, key: string, value: unknown) => {
116116
// @ts-ignore - we've validated these via runtime checks
117-
element[key] = value;
117+
if (target[key] !== value) {
118+
// @ts-ignore - we've validated these via runtime checks
119+
target[key] = value;
120+
}
118121
};
119122

120123
const sanitizeScripts = (node: Node): Node | null => {

0 commit comments

Comments
 (0)