Teact: Support useState init function (#6359)
This commit is contained in:
parent
85ab3ad1a8
commit
a93b99a9ac
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user