State is data held outside web components that can influence their rendered output or behavior. Shared state lets multiple components use one value without passing it through every level of a component hierarchy. section 7.11 section 8.11
One name, three components
A shared name value begins as "World". A labeled input displays and changes it, hello-world displays "Hello, World!", and my-app resets it when the button is clicked. When the value changes, the connected components update together. section 7.11
import { state } from "@microsoft/fast-element/state.js";
export const myState = state({ name: "World" });
The smallest FAST state object contains one shared name property with an initial value. section 7.11
Two implementations demonstrate the same coordination pattern:
- FAST components read myState.current.name, update the state with myState.set, and react automatically when the property changes. section 7.11
- Wrec creates a named WrecState instance and binds state.name to component properties; bindings can be one-way or two-way, and state may persist in sessionStorage. section 8.11
- Multiple state objects can group related shared properties, while WrecState objects can be retrieved by name and can expose subscriptions for selected paths. section 7.11 section 8.11
Listing 8.21 index.html
<!doctype html>
<html lang="en">
<head>
<style>
body {
font-family: sans-serif;
padding: 1rem;
}
</style>
<script type="module">
import { WrecState } from "wrec";
import "./hello-world.ts";
import "./labeled-input.ts";
const state = new WrecState("demo", { name: "World" });
const li = document.querySelector("labeled-input");
li.useState(state, { name: "value" });
const hw = document.querySelector("hello-world");
hw.useState(state);
const button = document.querySelector("button");
button.addEventListener("click", () => {
state.name = "World";
});
</script>
</head>
<body>
<labeled-input id="name" label="Name"></labeled-input>
<hello-world></hello-world>
<button>Reset</button>
</body>
</html>
The Wrec example shows named state creation, property mapping, component binding, and a reset handler in index.html. section 8.11



















































