State and actions
Share values between components with named states, and send named events with actions.
This page covers the two ways components in the
app
package talk to each other without holding references: states, which are named values any component can read and observe, and actions, which are named events carrying a value. This is go-app's state and action system, now part of WebGo's
app
package.
WebGo builds on both.
app.Model
, on
Pages, layouts and state
, is a typed wrapper over one state, and dynamic styles, on
Applying
, react to actions. The UI goroutine,
Dispatch
and
Async
are on
Concurrency and the browser
.
ยง1 State
func
(
ctx
Context
)
SetState
(
state
string
,
v
any
)
State
func
(
ctx
Context
)
GetState
(
state
string
,
recv
any
)
func
(
ctx
Context
)
DelState
(
state
string
)
A state is a value stored under a string key. There is one store per running app, and any component can read or write any key. The store is safe for concurrent use, so these methods can be called from any goroutine.
SetState
-
SetStatestoresvunder the key, replacing the previous value and its expiry. -
For every component observing the key, it queues a delivery on the UI goroutine: the value is copied into the observer's receiver, its
OnChangeruns, and the component re-renders. Observers do not see the value during theSetStatecall, only when the UI goroutine gets to it. -
It also posts an action named after the key, with
vas its value, so an action handler or a dynamic style can follow the state. -
It returns a
State, whose methods persist, expire or broadcast the value just set. They are under Persisting and broadcasting . -
A pointer is stored as the pointer. Readers get a copy of what it points to at the time they read, so changing the pointed-to value after
SetStatechanges what later readers get without notifying observers. CallSetStateagain after a change.
GetState
-
GetStatecopies the current value intorecv, which must be a pointer. If the stored value is a pointer, the value it points to is copied. -
The type
recvpoints to must be exactly the stored type. A state set withSetState("n", 1)can be read into an*intbut not an*int64. On a mismatch, or whenrecvis not a pointer,GetStatelogs the error throughapp.Logand leavesrecvunchanged. -
If the key was not set since the app started,
GetStatelooks for a persisted copy in local storage and decodes it intorecv. If there is none,recvis left as it was, so give it its default before the call. -
If the state has expired, it is deleted from memory and local storage, and
recvis left as it was. -
A state stored as
nilsets*recvto its zero value.
DelState
-
DelStateremoves the value from memory and from local storage. -
It does not notify observers and does not remove them. They keep their last value until the next
SetStateon the key. To reset what observers show, set a zero value instead.
type
greeting
struct
{
app
.
Compo
name
string
}
func
(
g
*
greeting
)
OnMount
(
ctx
app
.
Context
)
{
g
.
name
=
"guest"
ctx
.
GetState
(
"user-name"
,
&
g
.
name
)
}
func
(
g
*
greeting
)
Render
(
)
app
.
UI
{
return
app
.
Div
(
)
.
Body
(
app
.
P
(
)
.
Text
(
"Hello, "
+
g
.
name
)
,
app
.
Input
(
)
.
Value
(
g
.
name
)
.
OnChange
(
g
.
rename
)
,
)
}
func
(
g
*
greeting
)
rename
(
ctx
app
.
Context
,
e
app
.
Event
)
{
ctx
.
SetState
(
"user-name"
,
ctx
.
JSSrc
(
)
.
Get
(
"value"
)
.
String
(
)
)
}
This component reads the name once, when it mounts, so it does not see later changes made by other components. Observing state covers following a key.