useStateLazy<T> function
Adds local state to a DartFunctionComponent
by returning a StateHook with StateHook.value initialized to the return value of init
.
Example:
UseStateTestComponent(Map props) {
final count = useStateLazy(() {
var initialState = someExpensiveComputation(props);
return initialState;
}));
return react.div({}, [
count.value,
react.button({'onClick': (_) => count.set(0)}, ['Reset']),
react.button({'onClick': (_) => count.set((prev) => prev + 1)}, ['+']),
]);
}
Learn more: reactjs.org/docs/hooks-reference.html#lazy-initial-state.
Implementation
StateHook<T> useStateLazy<T>(T Function() init) {
final result = React.useState(allowInterop(init));
return StateHook._(result[0] as T, result[1] as _JsStateHookSetter);
}