123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import { Renderer, Args, StoryContext, StoryId, DecoratorApplicator } from '@storybook/types';
- interface Hook {
- name: string;
- memoizedState?: any;
- deps?: any[] | undefined;
- }
- interface Effect {
- create: () => (() => void) | void;
- destroy?: (() => void) | void;
- }
- type AbstractFunction = (...args: any[]) => any;
- declare class HooksContext<TRenderer extends Renderer, TArgs extends Args = Args> {
- hookListsMap: WeakMap<AbstractFunction, Hook[]>;
- mountedDecorators: Set<AbstractFunction>;
- prevMountedDecorators: Set<AbstractFunction>;
- currentHooks: Hook[];
- nextHookIndex: number;
- currentPhase: 'MOUNT' | 'UPDATE' | 'NONE';
- currentEffects: Effect[];
- prevEffects: Effect[];
- currentDecoratorName: string | null;
- hasUpdates: boolean;
- currentContext: StoryContext<TRenderer, TArgs> | null;
- renderListener: (storyId: StoryId) => void;
- constructor();
- init(): void;
- clean(): void;
- getNextHook(): Hook;
- triggerEffects(): void;
- addRenderListeners(): void;
- removeRenderListeners(): void;
- }
- declare const applyHooks: <TRenderer extends Renderer>(applyDecorators: DecoratorApplicator<TRenderer>) => DecoratorApplicator<TRenderer>;
- /**
- * Returns a memoized value.
- * @template T The type of the memoized value.
- * @param {() => T} nextCreate A function that returns the memoized value.
- * @param {any[]} [deps] An optional array of dependencies. If any of the dependencies change, the memoized value will be recomputed.
- * @returns {T} The memoized value.
- * @example
- * const memoizedValue = useMemo(() => {
- * return doExpensiveCalculation(a, b);
- * }, [a, b]);
- */
- declare function useMemo<T>(nextCreate: () => T, deps?: any[]): T;
- /** Returns a memoized callback.
- *
- * @template T The type of the callback function.
- * @param {T} callback The callback function to memoize.
- * @param {any[]} [deps] An optional array of dependencies. If any of the dependencies change, the memoized callback will be recomputed.
- * @returns {T} The memoized callback.
- *
- * @example
- * const memoizedCallback = useCallback(
- * () => {
- * doSomething(a, b);
- * },
- * [a, b],
- * );
- */
- declare function useCallback<T>(callback: T, deps?: any[]): T;
- /**
- * Returns a mutable ref object.
- *
- * @template T The type of the ref object.
- * @param {T} initialValue The initial value of the ref object.
- * @returns {{ current: T }} The mutable ref object.
- *
- * @example
- * const ref = useRef(0);
- * ref.current = 1;
- */
- declare function useRef<T>(initialValue: T): {
- current: T;
- };
- /**
- * Returns a stateful value and a function to update it.
- *
- * @template S The type of the state.
- * @param {(() => S) | S} initialState The initial state value or a function that returns the initial state value.
- * @returns {[S, (update: ((prevState: S) => S) | S) => void]} An array containing the current state value and a function to update it.
- *
- * @example
- * const [count, setCount] = useState(0);
- * setCount(count + 1);
- */
- declare function useState<S>(initialState: (() => S) | S): [S, (update: ((prevState: S) => S) | S) => void];
- /**
- * A redux-like alternative to useState.
- *
- * @template S The type of the state.
- * @template A The type of the action.
- * @param {(state: S, action: A) => S} reducer The reducer function that returns the new state.
- * @param {S | I} initialArg The initial state value or the initial argument for the init function.
- * @param {(initialArg: I) => S} [init] An optional function that returns the initial state value.
- * @returns {[S, (action: A) => void]} An array containing the current state value and a function to dispatch actions.
- *
- * @example
- * const initialState = { count: 0 };
- *
- * function reducer(state, action) {
- * switch (action.type) {
- * case 'increment':
- * return { count: state.count + 1 };
- * case 'decrement':
- * return { count: state.count - 1 };
- * default:
- * throw new Error();
- * }
- * }
- *
- * function Counter() {
- * const [state, dispatch] = useReducer(reducer, initialState);
- * return (
- * <>
- * Count: {state.count}
- * <button onClick={() => dispatch({ type: 'increment' })}>+</button>
- * <button onClick={() => dispatch({ type: 'decrement' })}>-</button>
- * </>
- * );
- * }
- */
- declare function useReducer<S, A>(reducer: (state: S, action: A) => S, initialState: S): [S, (action: A) => void];
- declare function useReducer<S, I, A>(reducer: (state: S, action: A) => S, initialArg: I, init: (initialArg: I) => S): [S, (action: A) => void];
- declare function useEffect(create: () => (() => void) | void, deps?: any[]): void;
- interface Listener {
- (...args: any[]): void;
- }
- interface EventMap {
- [eventId: string]: Listener;
- }
- declare function useChannel(eventMap: EventMap, deps?: any[]): (eventName: string, ...args: any) => void;
- declare function useStoryContext<TRenderer extends Renderer, TArgs extends Args = Args>(): StoryContext<TRenderer>;
- declare function useParameter<S>(parameterKey: string, defaultValue?: S): S | undefined;
- declare function useArgs<TArgs extends Args = Args>(): [
- TArgs,
- (newArgs: Partial<TArgs>) => void,
- (argNames?: (keyof TArgs)[]) => void
- ];
- declare function useGlobals(): [Args, (newGlobals: Args) => void];
- export { EventMap as E, HooksContext as H, Listener as L, useCallback as a, useChannel as b, useEffect as c, useGlobals as d, useMemo as e, useParameter as f, useReducer as g, useRef as h, useState as i, useStoryContext as j, applyHooks as k, useArgs as u };
|