@@ -22,50 +22,6 @@ Stores are created with `state.NewStore` and read or updated via `Get` and
2222` Set ` . Derived values can be defined with ` RegisterComputed ` or convenience
2323helpers like ` Map ` and ` Map2 ` .
2424
25- ## Example
25+ State stores drive UI updates in this example.
2626
27- ``` go
28- if store.Get (" double" ) == nil {
29- state.Map (store, " double" , " count" , func (v int ) int {
30- return v * 2
31- })
32- }
33-
34- func (c *StateManagementComponent ) Increment () {
35- if v , ok := c.Store .Get (" count" ).(int ); ok {
36- c.Store .Set (" count" , v+1 )
37- }
38- }
39- ```
40-
41- 1 . ` Map ` defines the derived value ` double ` based on ` count ` .
42- 2 . The ` Increment ` function reads ` count ` and updates it with ` Set ` .
43- 3 . Each change automatically updates parts of the UI that depend on these
44- values.
45-
46- ### Advanced computed values
47-
48- ` RegisterComputed ` provides full control for deriving data from multiple
49- dependencies. It accepts a ` Computed ` definition listing the keys to watch
50- and a function that receives their current values in a map. Use it when the
51- ` Map ` helpers are insufficient:
52-
53- ``` go
54- import " fmt"
55-
56- if store.Get (" profile" ) == nil {
57- store.RegisterComputed (state.NewComputed (
58- " profile" ,
59- []string {" first" , " last" , " age" },
60- func (m map [string ]any) any {
61- first , _ := m[" first" ].(string )
62- last , _ := m[" last" ].(string )
63- age , _ := m[" age" ].(int )
64- return fmt.Sprintf (" %s %s (%d )" , first, last, age)
65- },
66- ))
67- }
68- ```
69-
70- This example combines three fields into a single formatted string, but the
71- function can perform any transformation and return any type.
27+ @include :ExampleFrame:{code:"/examples/components/state_management_component.go", uri:"/examples/state"}
0 commit comments