UseEffect class

A ReactterHook that manages side-effect.

The side-effect logic into the callback function is executed when dependencies of ReactterHook argument has changes or context of ReactterContext trigger 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 willUnmount event:

UseEffect(() {
  print("Execute by 'didMount' event");

  return () {
    print("Execute by 'willUnmount' event");
  };
}, [], this);

RECOMMENDED: Use it on ReactterContext constructor:

class AppContext extends ReactterContext {
  late final state = UseState(false);

  AppContext() {
    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 AppContext extends ReactterContext with DispatchEffect {
  AppContext() {
    UseEffect(
      () => print("Prompt execution or state changed"),
      [state], this
    );
  }
}

If you not put ReactterContext on the context parameter, should to call dispose method to clear any UseEffect's events.

See also:

Inheritance

Constructors

UseEffect(Function callback, List<ReactterHook> dependencies, [ReactterContext? context])

Properties

callback Function
Function to control side-effect and effect cleanup.
final
context ReactterContext?
final
dependencies List<ReactterHook>
Hooks dependencies
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose() → void
listenHooks(List<ReactterHook> hooks) → void
Suscribes to all hooks given.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
update([Function? callback]) → void
First, invokes the subscribers callbacks of the willUpdate event.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

dispatchEffect DispatchEffect
no setter