Run a callback once when the component mounts. Cleaner than useEffect with empty deps.
import { useEffect } from 'react';
export function useMount(fn: () => void): void {
useEffect(() => {
fn();
}, []);
}Runs the callback once after the component mounts. Cleaner than useEffect with empty dependency array when you only need mount logic.
Analytics, focus an input, or load initial data.
useMount(() => document.getElementById('search')?.focus());