liveBook · Manning

State

As explained in Web Component Development with Modern Libraries and Tooling

The current data held by a component or application that can influence its behavior or rendered output.

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

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Explains initialization, reactive updates, bindings, persistence, named access, and subscriptions through small component examples.
Examples
Uses synchronized greeting and labeled-input components, reset buttons, shared name values, and a state-change subscription.
What it assumes
Assumes familiarity with web components and component properties while demonstrating shared state through complete small examples.
What you can do after
Build shared component data with FAST state objects or WrecState bindings, including updates, resets, persistence, and subscriptions.
How it connects
Connects state with web components, data binding, prop drilling, custom elements, events, and sessionStorage.

Also explained in

Compare how 52 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.