Skip to content

Instantly share code, notes, and snippets.

View codemile's full-sized avatar

Nick Foscarini codemile

View GitHub Profile
@codemile
codemile / ScaffoldEmotionCachProvider.tsx
Created August 20, 2023 14:05
Emotion provider for MUI that works in layout.tsx files of the app directory in NextJs 13
'use client';
import createCache from '@emotion/cache';
import {CacheProvider, EmotionCache} from '@emotion/react';
import {
SerializedStyles,
StyleSheet
} from '@emotion/utils/dist/declarations/types';
import {useServerInsertedHTML} from 'next/navigation';
import {FC, PropsWithChildren, useCallback, useMemo} from 'react';
@codemile
codemile / useBounceValue.tsx
Created July 17, 2022 16:21
React hook for bouncing a value back to it's default after a delay.
import {useEffect, useState} from 'react';
export const useBounceValue = <TValue,>(
newValue: TValue,
defaultValue: TValue,
delay: number
) => {
const [value, setValue] = useState<TValue>(defaultValue);
useEffect(() => {
@codemile
codemile / g7_currencies.json
Created June 8, 2022 21:04
A list of the G7 currencies.
{
"AED": "United Arab Emirates Dirham",
"AFN": "Afghan Afghani",
"ALL": "Albanian Lek",
"AMD": "Armenian Dram",
"ANG": "Netherlands Antillean Guilder",
"AOA": "Angolan Kwanza",
"ARS": "Argentine Peso",
"AUD": "Australian Dollar",
"AWG": "Aruban Florin",
@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);