UseState<T> class

A ReactterHook that manages a state.

Contains a value of type T which represents the current state.

When value is different to previous state, UseState execute update to notify context of ReactterContext that has changed and in turn executes onWillUpdate and onDidUpdate.

This example produces one simple UseState:

class AppContext extends ReactterContext {
  late final likesCount = UseState(0, this);
  late final followed = UseState(false, this);
  late final text = UseState("", this);
  late final user = UseState<User?>(null, this);
  late final posts = UseState(<Post>[], this);

  AppContext() {
    likesCount.value += 1;
    followed.value = followed.value!;
    text.value = "This is a text";
    user.value = User(name: "name");

    // It's need to force [update] because mantain the previous state
    // and the [context] not aware that this state has changed
    posts.update(() {
      posts.value.add(
          Post(
            user: user.value,
            text: text.value,
          ),
        );
    });
  }
}

See also:

Inheritance

Constructors

UseState(T initial, [ReactterHookManager? context])

Properties

hashCode int
The hash code for this object.
no setterinherited
initial ↔ T
The initial value in state.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value ↔ T
Get current value state
getter/setter pair

Methods

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
reset() → void
Reset the state to initial value
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