Skip to content

Instantly share code, notes, and snippets.

View MarekZeman91's full-sized avatar
๐Ÿ˜

Marek Zeman MarekZeman91

๐Ÿ˜
  • Marek Zeman
  • Czech Republic
View GitHub Profile
@MarekZeman91
MarekZeman91 / useFunction.ts
Last active October 30, 2025 21:45
React hook that allows you to have always up-to-date function without re-render trigger.
import { useCallback } from 'react';
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function useFunction<T extends Function>(fn: T): T {
const hook = useCallback<any>(function (this: any, ...args: any[]) {
return hook.fn.apply(this, args);
}, []);
hook.fn = fn;
return hook as T;
}
import { useEffect } from 'react'
import { useFunction } from './useFunction'
/**
* Hook to use native event listener
* @param eventName
* @param listener Does not have to be memorized
*/
export function useNativeEvent<K extends keyof GlobalEventHandlersEventMap>(
import { Dispatch, SetStateAction, useState } from 'react'
import { useConst } from './useConst'
import { useOnUnmount } from './useOnUnmount'
export function useDebouncedState<T>(initialValue: T, delay = 1): [T, Dispatch<SetStateAction<T>>] {
const [debouncedValue, setDebouncedValue] = useState(initialValue)
const api = useConst(() => {
let tempValue = initialValue
let timeout = 0