diff --git a/src/lib/teact/teact.ts b/src/lib/teact/teact.ts index 6f25aa1d1..cfd362d0f 100644 --- a/src/lib/teact/teact.ts +++ b/src/lib/teact/teact.ts @@ -594,8 +594,8 @@ function forceUpdateComponent(componentInstance: ComponentInstance) { } export function useState(): [T | undefined, StateHookSetter]; -export function useState(initial: T, debugKey?: string): [T, StateHookSetter]; -export function useState(initial?: T, debugKey?: string): [T, StateHookSetter] { +export function useState(initial: T | (() => T), debugKey?: string): [T, StateHookSetter]; +export function useState(initial?: T | (() => T), debugKey?: string): [T, StateHookSetter] { if (!renderingInstance.hooks) { renderingInstance.hooks = {}; } @@ -607,9 +607,10 @@ export function useState(initial?: T, debugKey?: string): [T, StateHookSetter const componentInstance = renderingInstance; if (byCursor[cursor] === undefined) { + const initValue = typeof initial === 'function' ? (initial as () => T)() : initial; byCursor[cursor] = { - value: initial, - nextValue: initial, + value: initValue, + nextValue: initValue, setter: (newValue: ((current: T) => T) | T) => { if (componentInstance.mountState === MountState.Unmounted) { return;