Skip to content

Instantly share code, notes, and snippets.

View codemile's full-sized avatar

Nick Foscarini codemile

View GitHub Profile
@codemile
codemile / useSafeState.tsx
Created March 19, 2021 11:10
Here is a useSafeState hook that ignores state changes after a component has been unmounted.
import {
DependencyList,
Dispatch,
MutableRefObject,
SetStateAction,
useCallback,
useEffect,
useRef,
useState
} from 'react';
@codemile
codemile / strings.ts
Created April 20, 2021 20:05
Some handy string functions written in TypeScript
/**
* "document_id" -> "document id"
* "documentId" -> "document id"
*/
export const toSpaces = (str: string): string =>
str && str
.replace(/[-_]/g, ' ')
.replace(/([A-Z])/g, ' $1');
/**
@codemile
codemile / useLogger.tsx
Created April 21, 2021 12:52
A simple logging hook for React
import {useMemo} from 'react';
/**
* Prevents React.DOM from replacing methods inside hooks.
*/
const ref = {
log: console.log,
error: console.error,
debug: console.debug
};
@codemile
codemile / useInterval.tsx
Last active December 30, 2021 20:17
A interval hook you can use in your ReactJS projects
import {DependencyList, useEffect, useMemo} from 'react';
export const useInterval = (
callback: () => void,
ms: number,
deps: DependencyList
) => {
const all_deps = useMemo(
() => [callback, ms, ...deps],
// eslint-disable-next-line react-hooks/exhaustive-deps
@codemile
codemile / Repeater.tsx
Created May 19, 2021 14:26
Repeater triggers the onRepeat event multiple times at the speed interval.
import {FC, useEffect} from 'react';
export interface RepeaterProps {
disabled?: boolean;
emitFirst?: boolean;
onRepeat: () => void;
speed: number;
@codemile
codemile / KeyPressed.tsx
Created May 19, 2021 15:01
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;
}
@codemile
codemile / useStateStorage.tsx
Created June 24, 2021 10:58
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);
@codemile
codemile / useDocVisible.tsx
Created June 25, 2021 10:35
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);
@codemile
codemile / useWndFocus.tsx
Created June 25, 2021 10:39
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 () => {
@codemile
codemile / useDebounce.tsx
Created June 29, 2021 14:53
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;
};