import produce from 'immer' ; import { useState , useCallback } from 'react' ; const reducer = produce ( ( draft , payload ) => { if ( typeof payload === 'undefined' ) return ; if ( typeof payload === 'object' ) { Object . assign ( draft , payload ) ; return ; } if ( typeof payload === 'function' ) { return payload ( draft ) ; } return payload ; } ) ; type PayloadFn < T > = ( draft : T ) => T | void | undefined | null ; type Payload < T > = Partial < T > | PayloadFn < T > ; export default function useImmerState < T > ( initialState ) { const [ state , setState ] = useState ( initialState ) ; const modifyState = useCallback ( ( payload ) => { setState ( ( state ) => reducer ( state , payload ) ) ; } , [ setState ] ) ; return [ state , modifyState ] ; }