import { AnyAction, SideEffect } from '@fluentui/state'; import * as React from 'react'; type Dispatch = (e: DispatchEvent, action: Action, ...args: Parameters) => void; type DispatchEffect = (e: DispatchEvent, prevState: State, nextState: State) => void; type DispatchEvent = React.SyntheticEvent | Event; export const useDispatchEffect = ( dispatchEffect: DispatchEffect, ): [Dispatch, SideEffect] => { const latestEffect = React.useRef>(dispatchEffect); const latestEvent = React.useRef(null); latestEffect.current = dispatchEffect; const dispatch = React.useCallback>((e, action, ...args) => { latestEvent.current = e; action(...args); latestEvent.current = null; }, []); const sideEffect = React.useCallback>((prevState, nextState) => { return latestEffect.current(latestEvent.current as DispatchEvent, prevState, nextState); }, []); return [dispatch, sideEffect]; };