Skip to content

Instantly share code, notes, and snippets.

View nathanhfoster's full-sized avatar
💭
Building progressive web applications

Nathan Foster nathanhfoster

💭
Building progressive web applications
View GitHub Profile
@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;
};
@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';
@rikukissa
rikukissa / POST.md
Last active February 20, 2024 07:30
React Hook prompting the user to "Add to homescreen" 🏠 #PWA #React
title slug createdAt language preview
React Hook prompting the user to "Add to homescreen"
react-hook-prompting-the-user-to-add
2018-11-29T20:35:02Z
en
Simple React Hook for showing the user a custom "Add to homescreen" prompt.

React Hook for showing custom "Add to homescreen" prompt

@scottopolis
scottopolis / splice-object-array.js
Last active January 31, 2023 06:54
Remove object from array of objects in Javascript
// we have an array of objects, we want to remove one object using only the id property
const apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];
// get index of object with id of 37
const removeIndex = apps.findIndex( item => item.id === 37 );
// remove object
apps.splice( removeIndex, 1 );