Here's an example of a TypeScript React hook that uses the useMedia
hook to detect whether the user has a device that is set to dark mode:
550 chars24 lines
The hook uses window.matchMedia()
to listen for changes in the user's system theme preference. If the user has their device set to dark mode, the hook returns true
for darkMode
, otherwise it returns false
. This hook can be used in your React components to conditionally render based on the user's system theme preference.
gistlibby LogSnag