UseEffect
UseEffect
is a RtHook that allows to manage side-effect, and its functionality is closely related to Lifecycle of the instance and its dependencies.
Syntax
UseEffect
accepts these arguments:
-
callback
: A function that performs side effects. This function is executed when thedependencies
argument changes or the instance triggerLifecycle.didMount
event. If thecallback
returns aFunction
(considers as an effect cleanup), it will be called before the next effect is run or the instance triggerLifecycle.willUnmount
event. -
dependencies
: A list of state(RtState
, learn about it here) that the effect depends on.
Properties & Methods
UseEffect
provides the following properties and methods:
-
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
UseEffect
can be initialized using the constructor class, e.g.:
In the example above, the UseEffect
hook is used to print the value
of the uCount
state every second and stop the timer when the value reaches 10
.
Running it inmediately
Sometime you may want to execute inmediately the UseEffect
is initialized, you can use UseEffect.runOnInit
to do that, e.g.:
In the example above, the UseEffect.runOnInit
hook is used to print the value
of the uCount
state inmediately and increment the value every second.