UseState
UseState
is a hook that allows to declare state variables and manipulate its value
, which in turn notifies about its changes to listeners.
Syntax
UseState
accepts this argument:
initialValue
: Initial value ofT
type that it will hold.
Properties & Methods
UseState
provides the following properties and methods:
value
: A getter/setter that allows to read and write its state.
-
Methods inherited from
RtState
(Learn more here):-
update
: A method to notify changes after run a set of instructions. -
refresh
: A method to force to notify changes. - *
bind
: A method to bind an instance to it. - *
unbind
: A method to unbind an instance to it. - *
dispose
: A method to remove all listeners and free resources.
-
Usage
Declaration
UseState
can be initialized using the constructor class:
Reading and writing the value
UseState
has a value
property that allows to read and write its state:
Updating the state
Use update
method to notify changes after run a set of instructions:
Use refresh
method to force to notify changes.
Listening to changes
When value
has changed, the UseState
will emit the following events(learn about it here):
Lifecycle.willUpdate
event is triggered before thevalue
change orupdate
,refresh
methods have been invoked.Lifecycle.didUpdate
event is triggered after thevalue
change orupdate
,refresh
methods have been invoked.
Example of listening to changes: