Loading Thucde.dev
Preparing the next view.
1 min read
Learn how to reliably detect if a user is on macOS using JavaScript.

const isMac = window.navigator.userAgentData ? window.navigator.userAgentData.platform === 'macOS' : /Mac/i.test(window.navigator.userAgent);
window is not available during server-side rendering or the initial render, it's important to access it inside a useEffect to ensure it's defined. const [isMac, setIsMac] = useState(false); useEffect(() => { const isMac = window.navigator.userAgentData ? window.navigator.userAgentData.platform === 'macOS' : /Mac/i.test(window.navigator.userAgent); setIsMac(isMac); }, []);
in React, Nextjs