Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created November 2, 2023 15:06
Show Gist options
  • Save GalindoSVQ/4ec270c1786ec44c689684e9382efd87 to your computer and use it in GitHub Desktop.
Save GalindoSVQ/4ec270c1786ec44c689684e9382efd87 to your computer and use it in GitHub Desktop.
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);
}
@GalindoSVQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment