Skip to content

Instantly share code, notes, and snippets.

@schneidmaster
Last active October 31, 2018 17:08
Show Gist options
  • Save schneidmaster/0a6aa5a65c753e851346b6130841d571 to your computer and use it in GitHub Desktop.
Save schneidmaster/0a6aa5a65c753e851346b6130841d571 to your computer and use it in GitHub Desktop.
Typescript definitions for React hooks
// Typescript definitions for React hooks
// Take with a grain of salt; these are a first attempt and I've only actually
// used a few so far :)
import { Context, RefObject } from 'react';
declare module 'react' {
export function useState<T>(initialValue: T): [T, (newState: T) => void];
export function useEffect(effect: Function, inputs?: Array<any>): void;
export function useMutationEffect(effect: Function, inputs?: Array<any>): void;
export function useLayoutEffect(effect: Function, inputs?: Array<any>): void;
export function useContext<T>(context: Context<T>): any;
export function useReducer<T>(
reducer: (state: T, action: Object) => T,
initialState: T,
initialAction?: Object,
): [T, (action: Object) => void];
export function useCallback(callback: Function, inputs: Array<any>): Function;
export function useMemo<T>(memoized: Function, inputs: Array<any>): T;
export function useRef<T>(initialValue?: any): RefObject<T>;
export function useImperativeMethods<T>(
ref: RefObject<T>,
createInstance: () => Object,
inputs?: Array<any>
): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment