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("Execute by state changed or 'didMount' event");
}, [state], this);
If the callback returns a function,
then UseEffect considers this as an effect cleanup
.
The effect cleanup
callback is executed, before callback is called
or context trigger LifeCycle.willUnmount
event:
UseEffect(() {
print("Execute by 'didMount' event");
return () {
print("Execute by 'willUnmount' event");
};
}, [], this);
RECOMMENDED: Use it on Object constructor:
class AppController {
late final state = UseState(false);
AppController() {
UseEffect(() {
print('state: ${state.value}');
}, [state], this);
Future.delayed(
const Duration(seconds: 1),
() {
state.value = !state.value;
},
);
}
}
If you need to execute the UseEffect's callback inmediately created, use dispatchEffect on context parameter:
UseEffect(
() => print("Prompt execution or state changed"),
[state],
UseEffect.dispatchEffect,
);
or use mixin DispatchEffect:
class AppController with DispatchEffect {
AppController() {
UseEffect(
() => print("Prompt execution or state changed"),
[state],
this,
);
}
}
If you not put instance on the context parameter, should to call dispose method to clear any UseEffect's events.
See also:
- ReactterState, it receives as dependencies.
- Inheritance
-
- Object
- ReactterHook
- UseEffect
Constructors
-
UseEffect(Function callback, List<
ReactterState> dependencies, [Object? context]) - A ReactterHook that manages side-effect.
Properties
- $ → _ReactterHookRegister
-
This variable is used to register ReactterHook
and attach the ReactterState that are defined here.
final
- callback → Function
-
Function to control side-effect and effect cleanup.
final
- context → Object?
-
It's used to specify the context in which the UseEffect hook is being used.
If a context is provided, the UseEffect hook will listen
for the
LifeCycle.didMount
andLifeCycle.willUnmount
events of the context and execute the callback method accordingly. If no context is provided, the hook will simply watch for changes in the specifieddependencies
.final -
dependencies
→ List<
ReactterState> -
It's used to store the states as dependencies of UseEffect.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceAttached → Object?
-
no setterinherited
- isDisposed → bool
-
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
attachTo(
Object instance) → void -
Attaches an object instance to this state.
override
-
detachInstance(
) → void -
Detaches an object instance to this state.
override
-
dispose(
) → void -
Called when this object is removed
override
-
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
-
update(
[covariant Function? callback]) → void -
Executes
callback
, and notify the listeners about to update.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- dispatchEffect → DispatchEffect
-
no setter