Shared State

Events

Simple Interactive Auto

Three components with no shared parent and no parameters between them read and write one piece of state.

How it works

Try it: click +1 and +2 and watch the render counters.

  • ClickState is a record implementing IState<ClickState>. The root hub creates it on first use from ClickState.Initial.
  • ClickButtons writes with State<ClickState>().Update(...). It never reads the state reactively, so it only re-renders after its own clicks (normal Blazor behavior for event handlers).
  • ClickDisplay declares [UseState] private partial ClickState Clicks { get; } and re-renders on every change.
  • ParityBadge uses a selector, SelectIsEven. +2 doesn't change parity, so it doesn't re-render.
  • Set same value writes an equal record. Record equality turns that into a no-op, so nothing re-renders.

Writer ClickButtons

renders: 1

Whole value [UseState]

0
renders: 1

Selector [UseState(Select = …)]

even
renders: 1
An unhandled error has occurred. Reload 🗙