Skip to content

Instantly share code, notes, and snippets.

@vkbansal
Last active January 11, 2022 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkbansal/239ad3d118b6b93708f265baf2539480 to your computer and use it in GitHub Desktop.
Save vkbansal/239ad3d118b6b93708f265baf2539480 to your computer and use it in GitHub Desktop.
string-ext-06
// StringsContext.tsx
export function useStringsContext(): Record<string, any> {
return React.useContext(StringsContext);
}
export interface UseLocaleStringsReturn {
getString(key: string): string;
}
export function useLocaleStrings() {
const strings = useStringsContext();
return {
getString(key: string): string {
if (key in strings) {
return strings[key];
}
throw new Error(`Strings data does not have a definition for: "${key}"`);
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment