Options
All
  • Public
  • Public/Protected
  • All
Menu

Module lib/hooks/useChangeHandlers

Index

References

Re-exports ChangeHandler

Functions

  • useChangeHandlers<T>(initialState: T): { state: T; handlers: ChangeHandlers<T>; setState: Dispatch<SetStateAction<T>>; mergeState: Dispatch<SetStateAction<Partial<T>>> }
  • This is a utility for taking a a set of fields that will be used in forms, change/click handlers, etc. to update state. It returns the initial state and the setState function like useState does, however it also generates a change handler for each field that is set up to work well with the ChangeHandler interface.

    Example:

    type Option = 'option1' | 'option2';
    interface MyState {
    field1: Option;
    }
    const MyForm = () => {
    const { state, handlers } = useChangeHandlers<MyState>({ field1: 'option1' });
    return <Select<Option> onChange={handlers.field1} />;
    }

    This is alpha stage, use only where it's easy to revert since the API may change significantly.

    alpha

    Type Parameters

    • T

    Parameters

    • initialState: T

    Returns { state: T; handlers: ChangeHandlers<T>; setState: Dispatch<SetStateAction<T>>; mergeState: Dispatch<SetStateAction<Partial<T>>> }

    • state: T
    • handlers: ChangeHandlers<T>
    • setState: Dispatch<SetStateAction<T>>
    • mergeState: Dispatch<SetStateAction<Partial<T>>>

Generated using TypeDoc