Match-id-196a52c4e94e9e29af3ba17f9477417aa762cd98
This commit is contained in:
parent
dfca1283c2
commit
83d0f2081b
|
@ -34,7 +34,7 @@ export function setCurrentHook(hook: Hook<any, any> | null) {
|
|||
currentHook = hook;
|
||||
}
|
||||
|
||||
export function throwNotInFuncError() {
|
||||
export function throwNotInFuncError(): never {
|
||||
throw Error('Hooks should be used inside function component.');
|
||||
}
|
||||
|
||||
|
|
|
@ -27,22 +27,25 @@ import { getProcessingVNode } from '../GlobalVar';
|
|||
import { Ref, Trigger } from './HookType';
|
||||
|
||||
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 {
|
||||
const processingVNode = getProcessingVNode();
|
||||
return getNewContext(processingVNode!, Context, true);
|
||||
}
|
||||
|
||||
export function useState<S>(initialState: (() => S) | S): [S, Dispatch<BasicStateAction<S>>] {
|
||||
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>>] {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -57,4 +57,4 @@ export type Ref<V> = {
|
|||
current: V;
|
||||
};
|
||||
|
||||
export type Trigger<A> = (A) => void;
|
||||
export type Trigger<A> = (state: A) => void;
|
||||
|
|
|
@ -17,7 +17,7 @@ import { createHook, getCurrentHook, throwNotInFuncError } from './BaseHook';
|
|||
import { getHookStage, HookStage } from './HookStage';
|
||||
import type { Ref } from './HookType';
|
||||
|
||||
export function useRefImpl<V>(value: V): Ref<V> {
|
||||
export function useRefImpl<V>(value?: V): Ref<V> {
|
||||
const stage = getHookStage();
|
||||
if (stage === null) {
|
||||
throwNotInFuncError();
|
||||
|
|
|
@ -21,6 +21,6 @@ function defaultReducer<S>(state: S, action: ((S) => S) | S): S {
|
|||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue