Teact: Support useState init function (#6359)

This commit is contained in:
zubiden 2025-10-12 15:07:57 +02:00 committed by Alexander Zinchuk
parent 85ab3ad1a8
commit a93b99a9ac

View File

@ -594,8 +594,8 @@ function forceUpdateComponent(componentInstance: ComponentInstance) {
}
export function useState<T>(): [T | undefined, StateHookSetter<T | undefined>];
export function useState<T>(initial: T, debugKey?: string): [T, StateHookSetter<T>];
export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter<T>] {
export function useState<T>(initial: T | (() => T), debugKey?: string): [T, StateHookSetter<T>];
export function useState<T>(initial?: T | (() => T), debugKey?: string): [T, StateHookSetter<T>] {
if (!renderingInstance.hooks) {
renderingInstance.hooks = {};
}
@ -607,9 +607,10 @@ export function useState<T>(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;