useState<T> function

StateHook<T> useState<T>(
  1. T initialValue
)

Adds local state to a DartFunctionComponent by returning a StateHook with StateHook.value initialized to initialValue.

Note: If the initialValue is expensive to compute, useStateLazy should be used instead.

Example:

UseStateTestComponent(Map props) {
  final count = useState(0);

  return react.div({}, [
    count.value,
    react.button({'onClick': (_) => count.set(0)}, ['Reset']),
    react.button({
      'onClick': (_) => count.setWithUpdater((prev) => prev + 1),
    }, ['+']),
  ]);
}

Learn more: reactjs.org/docs/hooks-state.html.

Implementation

StateHook<T> useState<T>(T initialValue) {
  final result = React.useState(initialValue);
  return StateHook._(result[0] as T, result[1] as _JsStateHookSetter);
}