overleaf/services/web/frontend/js/shared/hooks/use-safe-dispatch.ts
ilkin-overleaf 5b0c122f5d Merge pull request #7290 from overleaf/ii-7154-list-user-emails
List of user emails

GitOrigin-RevId: 28a8e405812932ba7ebd8043a4dc9d3c573a68b2
2022-04-11 08:03:38 +00:00

17 lines
384 B
TypeScript

import * as React from 'react'
import useIsMounted from './use-is-mounted'
function useSafeDispatch<T>(dispatch: React.Dispatch<T>) {
const mounted = useIsMounted()
return React.useCallback<(args: T) => void>(
action => {
if (mounted.current) {
dispatch(action)
}
},
[dispatch, mounted]
) as React.Dispatch<T>
}
export default useSafeDispatch