Created
November 2, 2023 15:06
-
-
Save GalindoSVQ/4ec270c1786ec44c689684e9382efd87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useSyncExternalStore } from 'react'; | |
| const subcribe = (cb: any) => { | |
| addEventListener('languagechange', cb); | |
| return () => removeEventListener('languagechange', cb); | |
| }; | |
| const getSnapshot = () => { | |
| return navigator.language; | |
| }; | |
| const getServerSnapshot = () => { | |
| throw Error('usePreferredLanguage is a client component'); | |
| }; | |
| export default function usePreferredLanguage() { | |
| return useSyncExternalStore(subcribe, getSnapshot, getServerSnapshot); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stackblitz Link: https://stackblitz.com/edit/vitejs-vite-hshkva?file=src%2FusePreferredLanguage.ts