UseEffect class
A ReactterHook that manages side-effect
.
The side-effect
logic into the callback function is executed
when dependencies of ReactterState argument has changes
or instance
trigger LifeCycle.didMount
event:
UseEffect(
() {
print("Executed by state changed or 'didMount' event");
},
[state],
);
The callback function can return a cleanup function to manage the effect cleanup
.
The cleanup function is executed before the dependencies of
ReactterState argument has changes or instance
trigger
LifeCycle.willUnmount
event:
UseEffect(
() {
print("Executed by state changed or 'didMount' event");
return () {
print("Executed before state changed or 'willUnmount' event");
};
},
[state],
);
RECOMMENDED: Use it on class constructor:
class AppController {
final state = UseState(0);
AppController() {
UseEffect(
() {
print("Executed by state changed or 'didMount' event");
return () {
print("Executed before state changed or 'willUnmount' event");
};
},
[state],
);
Timer.periodic(Duration(seconds: 1), (_) => state.value++);
}
}
If you want to execute the effect on initialization, you can use UseEffect.runOnInit:
UseEffect.runOnInit(
() {
print("Executed on initialization and 'didMount' event");
},
[],
);
You can also use the DispatchEffect mixin to execute the effect on initialization:
class AppController with DispatchEffect {
AppController() {
UseEffect(
() {
print("Executed on initialization and 'didMount' event");
},
[],
);
}
}
Use bind method to bind the instance, if you want to execute the effect within the instance context:
UseEffect(
() {
print("Executed by `didMount` event of 'useController' instance");
},
[],
).bind(useController);
NOTE: A UseEffect instance can only be binded to one instance at a time. When create a new instance of UseEffect on the instance that is created by dependency injection, this instance will be binded to it automatically.
NOTE: The UseEffect instance will be disposed automatically when the instance binded is destroyed by dependency injection.
If the UseEffect instance didn't have an instance binded or the instance binded is not created by dependency injection, you must dispose it manually, using the dispose method:
final useEffect = UseEffect(
() {
print("Executed by state changed");
},
[state],
);
[...]
useEffect.dispose();
See also:
- ReactterState, it receives as dependencies.
- Inheritance
-
- Object
- ReactterHook
- UseEffect
Constructors
-
UseEffect(Function callback, List<
ReactterState> dependencies) -
A ReactterHook that manages
side-effect
. -
UseEffect.runOnInit(Function callback, List<
ReactterState> dependencies) -
A ReactterHook that manages
side-effect
.
Properties
- $ → HookRegister
-
This variable is used to register
Hook
and attach theStateBase
that are defined here.final - callback → Function
-
Function to control side-effect and effect cleanup.
final
-
dependencies
→ List<
ReactterState> -
It's used to store the states as dependencies of UseEffect.
final
- dependencyInjection → DependencyInjection
-
no setterinherited
- eventHandler → EventHandler
-
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceBinded → Object?
-
The reference instance to the current state.
no setterinherited
- isDisposed → bool
-
Returns
true
if the state has been disposed.no setterinherited - logger → Logger
-
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stateManagment
→ StateManagement<
StateBase> -
no setterinherited
Methods
-
bind(
Object instance) → void - Stores a reference to an object instance
-
dispose(
) → void - Called when this object is removed
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refresh(
) → void -
It's used to notify listeners that the state has been updated.
It is typically called after making changes to the state object.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
unbind(
) → void - Removes the reference to the object instance
-
update(
[covariant Function? callback]) → void -
Executes
callback
, and notifies the listeners about the update.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited