import React from 'react' ; import useState from './useState' ; import useRef from './useRef' ; export default function useReducer < R extends React . ReducerWithoutAction < any > , I > ( reducer , initializerArg , initializer ) ; export default function useReducer < R extends React . ReducerWithoutAction < any > > ( reducer , initializerArg , initializer ? ) ; export default function useReducer < R extends React . Reducer < any , any > , I > ( reducer , initializerArg , initializer ) ; export default function useReducer < R extends React . Reducer < any , any > , I > ( reducer , initializerArg , initializer ) ; export default function useReducer < R extends React . Reducer < any , any > > ( reducer , initialState , initializer ? ) ; export default function useReducer ( reducer , initialArg , init ? ) { let initialState ; if ( init instanceof Function ) { initialState = init ( initialArg ) ; } else { initialState = initialArg ; } const [ state , setState ] = useState ( initialState ) ; const stateRef = useRef ( ) ; stateRef . current = state ; const dispatch = function ( this , action ) { const newState = reducer ( this . current , action ) ; setState ( newState ) ; } . bind ( stateRef ) ; return [ state , dispatch ] ; }