/
githubmirror
/
ydb-embedded-ui
Обзор
Документация
Войти
/
githubmirror
/
ydb-embedded-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/utils/hooks/useEventHandler.ts
17 строк
606 B
Valerii Sidorenko
refactor(QueryEditor): get rid of some misused useEffects (#1107)
02 авг 2024, 18:48
Не верифицирован
02 авг 2024, 18:48
cc51b20
Код
Авторство
О чём код?
import React from 'react'; /** * The hook returns a stable function (an empty list of dependencies), * but this function always calls the actual function associated with the last render. * The returned function should be used as an event handler or inside a useEffect. */ export function useEventHandler<T extends Function>(handler?: T) { const ref = React.useRef<T | undefined>(handler); React.useLayoutEffect(() => { ref.current = handler; }, [handler]); // @ts-expect-error return React.useCallback<T>((...args) => { return ref.current?.(...args); }, []); }