UNPKG

@figliolia/react-hooks

Version:

A small collection of simple React Hooks you're probably rewriting on a regular basis

23 lines (17 loc) 596 B
import { useCallback, useEffect, useState } from "react"; const getLocale = (defaultLocale: string) => { return window?.navigator?.language ?? defaultLocale; }; export const useLocale = (defaultLocale = "en-us") => { const [locale, setLocale] = useState(getLocale(defaultLocale)); const onChange = useCallback(() => { setLocale(getLocale(defaultLocale)); }, [defaultLocale]); useEffect(() => { window.addEventListener("languagechange", onChange); return () => { window.removeEventListener("languagechange", onChange); }; }, [onChange]); return locale; };