Skip to content

Instantly share code, notes, and snippets.

View abonello's full-sized avatar

Anthony Bonello abonello

View GitHub Profile
@abonello
abonello / KeyPressed.tsx
Created December 30, 2021 20:16 — forked from codemile/KeyPressed.tsx
React component that handles pressed/released states for a single key on the keyboard.
import {FC, useEffect} from 'react';
export interface KeyPressedProps {
keyCode: string;
onPress: () => void;
onRelease: () => void;
}
@abonello
abonello / useStateStorage.tsx
Created December 30, 2021 20:16 — forked from codemile/useStateStorage.tsx
A hook that persists useState() to localStorage.
import {useCallback, useEffect, useState} from 'react';
export const useStateStorage = <TType extends any>(
key: string,
defaultValue: TType
) => {
const [value, setState] = useState(defaultValue);
useEffect(() => {
const store = localStorage.getItem(key);
@abonello
abonello / useDocVisible.tsx
Created December 30, 2021 20:16 — forked from codemile/useDocVisible.tsx
Hook that emits document visibility.
import {useEffect, useState} from 'react';
export const useDocVisible = () => {
const isVisible = () => document.visibilityState === 'visible';
const [visible, setVisible] = useState(isVisible());
useEffect(() => {
const onVisible = () => setVisible(isVisible());
document.addEventListener('visibilitychange', onVisible);
return () =>
document.removeEventListener('visibilitychange', onVisible);
@abonello
abonello / useWndFocus.tsx
Created December 30, 2021 20:15 — forked from codemile/useWndFocus.tsx
Hook that emits when the window has focus.
import {useEffect, useState} from 'react';
export const useWndFocus = () => {
const [focus, setFocus] = useState(document.hasFocus());
useEffect(() => {
const onFocus = () => setFocus(true);
const onBlur = () => setFocus(false);
window.addEventListener('focus', onFocus);
window.addEventListener('blur', onBlur);
return () => {
@abonello
abonello / useDebounce.tsx
Created December 30, 2021 20:15 — forked from codemile/useDebounce.tsx
Hook that debounces values by discarding inputs that take less than the specified milliseconds between changes.
import {useEffect, useState} from 'react';
export const useDebounce = <TType extends any>(next: TType, ms: number) => {
const [value, setValue] = useState(next);
useEffect(() => {
const id = setTimeout(() => setValue(next), ms);
return () => clearTimeout(id);
}, [next, ms]);
return value;
};
@abonello
abonello / useStatePair.tsx
Created December 30, 2021 20:15 — forked from codemile/useStatePair.tsx
Hook that emits the previous and current values of useState()
import {useCallback, useState} from 'react';
export const useStatePair = <TType extends any>(initialState?: TType) => {
const [[prev, current], setPair] = useState<Array<TType | undefined>>([
undefined,
initialState
]);
const setValue = useCallback(
(next: TType) => setPair([current, next]),
[current]
@abonello
abonello / useStateCount.tsx
Created December 30, 2021 20:14 — forked from codemile/useStateCount.tsx
Hook that increments a counter every time useState() is changed.
import {useCallback, useState} from 'react';
export const useStateCount = <TType extends any>(initialState?: TType) => {
const [[count, value], setState] = useState([0, initialState]);
const setValue = useCallback(
(next: TType) => setState([count + 1, next]),
[count]
);
return [count, value, setValue];
};
@abonello
abonello / useStateBuffer.tsx
Created December 30, 2021 20:14 — forked from codemile/useStateBuffer.tsx
Hook that records the history of state changes as an Array buffer upto a certain length.
import {useCallback, useState} from 'react';
export const useStateBuffer = <TType extends any>(
size: number,
initialState: TType
) => {
const [values, setState] = useState<Array<TType>>([initialState]);
const setValue = useCallback(
(next: TType) => setState([...values, next].slice(0, size)),
[values]
@abonello
abonello / useStateJson.tsx
Created December 30, 2021 20:14 — forked from codemile/useStateJson.tsx
Hook that stores a state value as a JSON string to prevent render updates when an object value is mutated but doesn't change.
import {useCallback, useMemo, useState} from 'react';
export const useStateJson = <TType extends any>(initialState?: TType) => {
const [json, setState] = useState<string>(
JSON.stringify(initialState ?? '')
);
const value = useMemo(() => JSON.parse(json), [json]);
const setValue = useCallback(
(next: TType) => setState(JSON.stringify(next ?? '')),
[]
@abonello
abonello / useStateDebounce.tsx
Created December 30, 2021 20:13 — forked from codemile/useStateDebounce.tsx
Hook that debounces useState() changes.
import {useCallback, useEffect, useRef, useState} from 'react';
export const useStateDebounce = <TType extends any>(ms: number, initialState?: TType) => {
const [state, setState] = useState(initialState);
const ref = useRef(0);
const setValue = useCallback(value => {
window.clearTimeout(ref.current);
ref.current = window.setTimeout(() => setState(value), ms);
}, [ms]);
useEffect(() => () => window.clearTimeout(ref.current), []);