Skip to content

Instantly share code, notes, and snippets.

View officialdavidtaylor's full-sized avatar

David Taylor officialdavidtaylor

View GitHub Profile
@officialdavidtaylor
officialdavidtaylor / catchall map example.js
Last active April 2, 2025 22:19
A technique for constructing a JS map object with a default return value
// example inspired by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
const defaultKey = Symbol("default");
// a benefit of keying with a Symbol is that it is not enumerable in a for...in iteration
const map = {
key1: "foo",
key2: "bar",
[defaultKey]: "baz",
@officialdavidtaylor
officialdavidtaylor / useCurrentTime.ts
Created April 17, 2024 14:50
A React Custom Hook to opt the component (and all downstream components) into re-renders that occur on a specified interval.
import { useRef, useState } from 'react';
/**
* This hook will opt your component (and all downstream components) into
* rerenders that occur on the interval specified.
*
* ⚠️ This may induce serious performance consequences, especially when
* choosing a shorter intervalDuration
*
* @param intervalDuration number (in ms), default to 1000ms