#Using a global variable throughout .tsx and .ts files

19 messages · Page 1 of 1 (latest)

tawdry saffron
#

using the uuid library, I want to generate a uuid (called sessionID) that can be referenced anywhere in my application (including functions that make database requests through prisma and actual pages of my app), and only updates under certain circumstances (ex. logging in). Is there a way to implement this? I've tried using getServerSideProps in the files that require sessionID but I haven't got it to work. Would greatly appreciate any help!

raven sparrowBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

fallen marsh
tawdry saffron
fallen marsh
tawdry saffron
#

oh sick, I'll try it out soon and lyk how it goes

fallen marsh
tawdry saffron
rocky quail
#

You should make a context provider in layout that gets the cookies and exposes it down stream.

#

Easiest and most flexible method to achieving what you want.

tawdry saffron
rocky quail
#

I would lookup how to make a context provider there’s more than that but I’m in the pool and can’t type it up 😂

fallen marsh
# tawdry saffron sorry for the long wait, but I tried implementing this and got this error when c...

The cookies function is only available serverside. As Jbomcz said you can either use a context provider to get the value serverside and pass it to the client or you can read the cookies clientside as well with the help of additional packages like: https://www.npmjs.com/package/cookies-next

If you still want to use the context method, this might be what you looking for: https://www.npmjs.com/package/next-client-cookies
Of course you can also archive that functionality without using a library for that. Then take the second link as guide and just add the cookies to the context

tawdry saffron
#

having some confusion here with cookies-next:

when I try to set and get the cookie in the same file, getCookie() returns undefined; I know the sessionID exists, I just can't understand why its not being saved with setCookie or why it's being saved as undefined. Any clarity would be greatly appreciated

fallen marsh
tawdry saffron
#

nope

#

unless I'm missing smth

fallen marsh
#

then that's the problem. To be honest I never worked with the package, so I cant help you with that