useMergeState

    API

    import useMergeState from '@restart/hooks/useMergeState'
    • useMergeState<TState>(initialState: TState | () => TState) => [TState,MergeStateSetter<TState>]

      Mimics a React class component's state model, of having a single unified state object and an updater that merges updates into the existing state, as opposed to replacing it.

      const [state, setState] = useMergeState({ name: 'Betsy', age: 24 })
      setState({ name: 'Johan' }) // { name: 'Johan', age: 24 }
      setState(state => ({ age: state.age + 10 })) // { name: 'Johan', age: 34 }

      Parameters

      • initialStateTState | () => TState

        The initial state object

      Return Value [TState,MergeStateSetter<TState>]