Skip to content
Kite

Kites: share state between components

Use extras/kites when several screens read and write the same application data. A button’s hover state can stay local; a team’s task count should have one owner even if it appears on both the board and the dashboard.

Create the store once, not during each render. Keep data in the snapshot and behavior in methods. Here Bind turns a method that needs a team name into a mutator that Update can apply:

type Counters struct{ ByTeam map[string]int }
func (s *Counters) Increment(team string) { s.ByTeam[team]++ }
func (s Counters) Count(team string) int { return s.ByTeam[team] }
var store = kites.Create(Counters{ByTeam: map[string]int{}})
func creditBothTeams() {
store.Update(
kites.Bind((*Counters).Increment, "docs"),
kites.Bind((*Counters).Increment, "tools"),
)
}

Both increments run in order on one writable draft under one lock. Subscribers are notified once, after the lock is released; they never see a half-finished batch. Treat snapshots from Get, hooks, and subscribers as read-only, including their maps and slices. Change the draft inside Update or Set, not a map saved from an earlier read.

A button that shows the docs count doesn’t need to render again when only the tools count changes. kites.Select binds the team name to the selector; kites.Use reads its result and subscribes. store.Action builds a handler during render but waits until the press to update:

var docsButton = kitex.FC("DocsCount", func(kitex.NoProps) kitex.Node {
count := kites.Use(store, kites.Select(Counters.Count, "docs"))
press := store.Action(kites.Bind((*Counters).Increment, "docs"))
return kitex.Button(kitex.ButtonProps{OnPress: press}, kitex.Textf("Docs: %d", count))
})

Use store.Use() when the component needs the entire snapshot. For a selector returning a map, the default comparison uses reflect.DeepEqual. Use kites.UseWith when a different comparison matches the display: this summary treats an empty map and a nil map alike.

var summary = kitex.FC("TeamSummary", func(kitex.NoProps) kitex.Node {
counts := kites.UseWith(store, func(s Counters) map[string]int { return s.ByTeam },
maps.Equal[map[string]int, map[string]int])
return kitex.Textf("Team counts: %v", counts)
})

No hook is needed for a background observer. Subscribe receives every write as (next, prev); SubscribeSelector calls its listener only when the selected value changes. Keep the unsubscribe functions for the lifetime of the observer:

func observe(ctx context.Context, record func(Counters, Counters), reportDocs func(int, int)) {
stopAll := store.Subscribe(record)
defer stopAll()
stopDocs := kites.SubscribeSelector(store, kites.Select(Counters.Count, "docs"), reportDocs)
defer stopDocs()
<-ctx.Done()
}

Declare the context once. Use kites.UseContext to subscribe to a value from the nearest provider. An action-only button can call ctx.Store() during render to get the active store without subscribing to its writes:

var ctx = kites.NewContext(Counters{ByTeam: map[string]int{}})
var docsLabel = kitex.FC("DocsLabel", func(kitex.NoProps) kitex.Node {
count := kites.UseContext(ctx, kites.Select(Counters.Count, "docs"))
return kitex.Textf("Docs: %d", count)
})
var clearButton = kitex.FC("ClearCounts", func(kitex.NoProps) kitex.Node {
return kitex.Button(kitex.ButtonProps{OnPress: ctx.Store().Reset}, kitex.Text("Reset counts"))
})
func board(s *kites.Store[Counters]) kitex.Node {
return ctx.Provider(s, kitex.Fragment(docsLabel(), clearButton()))
}

Without a provider, both components use the context’s default store. For test isolation, render board(kites.Create(Counters{ByTeam: map[string]int{"docs": 3}})) with a fresh store in each test instead of resetting shared state between tests.

After installing DevTools, register the Stores panel with your inspector and track the instance your UI actually uses. Import the bridge from extras/kites/kitesdt:

func inspectCounters(insp *inspector.Inspector, s *kites.Store[Counters]) {
kitesdt.Register(insp)
kitesdt.Track("Team counters", s)
}

Press the docs button and look for Increment in the history: Bind preserves the method name. Compare the previous and next snapshots, or restore an earlier entry to see how the subscribed UI responds. Call kitesdt.Untrack(s) when a short-lived store is no longer needed.

Task API Effect
Read once store.Get() Returns the current snapshot without subscribing.
Mutate in place store.Update(mutators...) Commits one draft for the batch.
Transform store.Set(func(s Counters) Counters { delete(s.ByTeam, "docs"); return s }) Starts from the current draft and commits the returned value.
Replace store.SetState(next) Uses the supplied state rather than transforming the current one.
Restore defaults store.Reset() Returns to the initial snapshot passed to Create.
Read during render kites.Use, kites.UseWith, store.Use() Subscribes to a selection or the whole snapshot.
Observe outside render store.Subscribe, kites.SubscribeSelector Returns a function that stops the subscription.

Try it in the task manager →