Skip to content

Instantly share code, notes, and snippets.

@lukemcdonald
lukemcdonald / PolymorphicComponent.tsx
Last active January 14, 2024 13:59
Type-Safe Polymorphic Component in React with TypeScript
import { Link } from "react-router-dom";
type AllowedElements = 'div' | typeof Link
interface PolymorphicComponentProps<T extends React.ElementType> {
as?: T extends AllowedElements ? T : never
className?: string
}
function PolymorphicComponent<T extends React.ElementType = 'div'>({
@lukemcdonald
lukemcdonald / useLocalStorage.tsx
Created May 22, 2023 18:06
React: useLocalStorage hook with debounce
import React from 'react';
/**
* localStorage works just like useState, except it backs up to (and restores from) localStorage.
*
* @param initialState The initial value to use
* @param key The local storage key to use
* @param options Optional. Currently allows a timeout (in milliseconds) to debouce the setting localStorage if needed.
* @returns The current value of the local storage item state, and a function to set it
*/
@lukemcdonald
lukemcdonald / changelog.sh
Last active April 27, 2023 19:34
Shell script to list changes for latest tag compared to head.
#!/usr/bin/env bash
set -euo pipefail
# Define the expected pattern.
EXPECTED_TAG_PATTERN="^v.+\..+$"
EXPECTED_TAG_PATTERN_EXAMPLE="v1.2.3"
# Check if git is installed
if ! command -v git >/dev/null 2>&1; then
printf >&2 "git is not installed. Aborting.\n"