useState<T> function

ValueNotifier<T> useState <T>([T initialData ])

Create variable and subscribes to it.

Whenever ValueNotifier.value updates, it will mark the caller HookWidget as needing build. On first call, inits ValueNotifier to initialData. initialData is ignored on subsequent calls.

The following example showcase a basic counter application.

class Counter extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final counter = useState(0);

    return GestureDetector(
      // automatically triggers a rebuild of Counter widget
      onTap: () => counter.value++,
      child: Text(counter.value.toString()),
    );
  }
}

See also:

Implementation

ValueNotifier<T> useState<T>([T initialData]) {
  return Hook.use(_StateHook(initialData: initialData));
}