Match-id-196a52c4e94e9e29af3ba17f9477417aa762cd98

This commit is contained in:
* 2023-07-10 17:20:59 +08:00
parent dfca1283c2
commit 83d0f2081b
5 changed files with 12 additions and 9 deletions

View File

@ -34,7 +34,7 @@ export function setCurrentHook(hook: Hook<any, any> | null) {
currentHook = hook; currentHook = hook;
} }
export function throwNotInFuncError() { export function throwNotInFuncError(): never {
throw Error('Hooks should be used inside function component.'); throw Error('Hooks should be used inside function component.');
} }

View File

@ -27,22 +27,25 @@ import { getProcessingVNode } from '../GlobalVar';
import { Ref, Trigger } from './HookType'; import { Ref, Trigger } from './HookType';
type BasicStateAction<S> = ((S) => S) | S; type BasicStateAction<S> = ((S) => S) | S;
type Dispatch<A> = (A) => void; type Dispatch<A> = (value: A) => void;
export function useContext<T>(Context: ContextType<T>): T { export function useContext<T>(Context: ContextType<T>): T {
const processingVNode = getProcessingVNode(); const processingVNode = getProcessingVNode();
return getNewContext(processingVNode!, Context, true); return getNewContext(processingVNode!, Context, true);
} }
export function useState<S = undefined>(): [S | undefined, Dispatch<BasicStateAction<S | undefined>>]
export function useState<S>(initialState: (() => S) | S): [S, Dispatch<BasicStateAction<S>>] { export function useState<S>(initialState: (() => S) | S): [S, Dispatch<BasicStateAction<S>>]
export function useState<S>(initialState?: (() => S) | S): [S, Dispatch<BasicStateAction<S>>] {
return useStateImpl(initialState); return useStateImpl(initialState);
} }
export function useReducer<S, I, A>(reducer: (S, A) => S, initialArg: I, init?: (I) => S): [S, Trigger<A>] | void { export function useReducer<S, I, A>(reducer: (S, A) => S, initialArg: I, init?: (I) => S): [S, Trigger<A>] {
return useReducerImpl(reducer, initialArg, init); return useReducerImpl(reducer, initialArg, init);
} }
export function useRef<T>(initialValue: T): Ref<T> { export function useRef<T = undefined>(): Ref<T | undefined>
export function useRef<T>(initialValue: T): Ref<T>
export function useRef<T>(initialValue?: T): Ref<T> {
return useRefImpl(initialValue); return useRefImpl(initialValue);
} }

View File

@ -57,4 +57,4 @@ export type Ref<V> = {
current: V; current: V;
}; };
export type Trigger<A> = (A) => void; export type Trigger<A> = (state: A) => void;

View File

@ -17,7 +17,7 @@ import { createHook, getCurrentHook, throwNotInFuncError } from './BaseHook';
import { getHookStage, HookStage } from './HookStage'; import { getHookStage, HookStage } from './HookStage';
import type { Ref } from './HookType'; import type { Ref } from './HookType';
export function useRefImpl<V>(value: V): Ref<V> { export function useRefImpl<V>(value?: V): Ref<V> {
const stage = getHookStage(); const stage = getHookStage();
if (stage === null) { if (stage === null) {
throwNotInFuncError(); throwNotInFuncError();

View File

@ -21,6 +21,6 @@ function defaultReducer<S>(state: S, action: ((S) => S) | S): S {
return typeof action === 'function' ? action(state) : action; return typeof action === 'function' ? action(state) : action;
} }
export function useStateImpl<S>(initArg: (() => S) | S): [S, Trigger<((S) => S) | S>] { export function useStateImpl<S>(initArg?: (() => S) | S): [S, Trigger<((S) => S) | S>] {
return useReducerImpl(defaultReducer, initArg, undefined, true); return useReducerImpl(defaultReducer, initArg, undefined, true);
} }