manifold/web/hooks/use-event.ts
James Grugett 96db414ca1
Category checklist (#426)
* Use ChoicesToggleGroup for categories vs following

* Edit categories modal

* Filter closed and resolved using Configure. Set page to 0.

* Add useEvent hook, incase we want to use it before React releases it.

* useMemo on filters computation

* Try to fix prettier

* Use check box! Add select all/none button
2022-06-05 14:06:08 -05:00

21 lines
656 B
TypeScript

// A hook soon to be added to the React core library:
// https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md
// TODO: Once React adds this hook, use it instead.
import { useRef, useLayoutEffect, useCallback } from 'react'
type AnyFunction = (...args: any[]) => any
export function useEvent<T extends AnyFunction>(callback?: T) {
const ref = useRef<AnyFunction | undefined>(() => {
throw new Error('Cannot call an event handler while rendering.')
})
useLayoutEffect(() => {
ref.current = callback
})
return useCallback<AnyFunction>(
(...args) => ref.current?.apply(null, args),
[]
) as T
}