DEV Community

Discussion on: useContext() with Typescript

Collapse
jedijetskyjodajedoindajaus profile image
jedidiah

It doesn't work for me, i don't have a getCopy, what this function does? could you please help me or get it touch with me!

Collapse
madv profile image
Amateur blogger Author

Hey! Thank you for your message and sorry for the inconvenience caused.
Basically, getCopy() method could be anything whose result you want to set globally. In my case, it is a simple method which gets content based on the logged in user type.
For eg:

const getCopy = (userType: string):string => 
{
   if (userType.toLowerCase() === 'admin')
   {
      return 'Hello Admin User!'
   }
   return 'Welcome user!'
}
Enter fullscreen mode Exit fullscreen mode
Collapse
enough7 profile image
Enough7 • Edited on

You can use localstorage for this:

const USER_ID_KEY: string = "userId";
export let thisUser: IUser = {
    id: parseInt(localStorage.getItem(USER_ID_KEY) || '-1') 
};
Enter fullscreen mode Exit fullscreen mode
Collapse
madv profile image
Amateur blogger Author

Thank you for the alternative, however what if the user has disabled his localStorage? ;)

Thread Thread
enough7 profile image
Enough7

I did not know you can disable it