UNPKG

884 BTypeScriptView Raw
1declare type Updater<TState> = (state: TState) => Partial<TState> | null;
2/**
3 * Updates state, partial updates are merged into existing state values
4 */
5export declare type MergeStateSetter<TState> = (update: Updater<TState> | Partial<TState> | null) => void;
6/**
7 * Mimics a React class component's state model, of having a single unified
8 * `state` object and an updater that merges updates into the existing state, as
9 * opposed to replacing it.
10 *
11 * ```js
12 * const [state, setState] = useMergeState({ name: 'Betsy', age: 24 })
13 *
14 * setState({ name: 'Johan' }) // { name: 'Johan', age: 24 }
15 *
16 * setState(state => ({ age: state.age + 10 })) // { name: 'Johan', age: 34 }
17 * ```
18 *
19 * @param initialState The initial state object
20 */
21export default function useMergeState<TState extends {}>(initialState: TState | (() => TState)): [TState, MergeStateSetter<TState>];
22export {};
23
\No newline at end of file