Tutorial: Full Name (Computed)
You'll learn: derived values with computed. Time: 5 minutes.
import { html, mount, createState, computed } from "../src/index.js";
function FullName() {
const first = createState("Jane");
const last = createState("Doe");
const fullName = computed(() => `${first.get()} ${last.get()}`.trim());
return html`
<div>
<label>First <input value=${() => first.get()} oninput=${(e) => first.set(e.target.value)} /></label>
<label>Last <input value=${() => last.get()} oninput=${(e) => last.set(e.target.value)} /></label>
<p>Full name: <strong>${() => fullName.get()}</strong></p>
</div>
`;
}
mount(FullName, document.querySelector("#app"));How it works#
fullNameis derived fromfirstandlast. You neversetit.- Editing either input updates the relevant state;
fullNamerecomputes automatically and the<strong>updates. fullNameis lazy and cached — it only recomputes whenfirstorlastactually change.
Why not just inline the expression?#
You could write ${() => first.get() + " " + last.get()} directly. Use computed when:
- you need the value in more than one place, or
- it's expensive (so you want caching), or
- other computeds build on it.
Try it yourself#
- Add an
initialscomputed:computed(() => first.get()[0] + last.get()[0]). - Add a
greetingcomputed that readsfullName(computeds can compose). - Show "even"/"odd" length of the full name with another computed.
Next: Reorderable list »