[React/NextJS] Any React Devs that can help figure out where we've gone short with this Cart?
https://github.com/callum-laing/shopping-site/tree/main
We're using localStorage and useEffect.. I think we're close, but we can't quite figure out the final hurdle to get over the finish line...
We're working in app/cart/page.jsx and app/shop/page.jsx.
78 Replies
pushes this to the top
Something like this 🤔
In my mind this works 😂
I should find a playground to just put your code in
mmm yea idk all sorts of issues when I try it in stack blitz
use client
seems like bs 🤣
casue I had to check to see if window exists to use localStorage which is fine but I thought that is what the whole use client string was for.
cause its trying to run ont he server first.
https://stackblitz.com/edit/stackblitz-starters-rtgg9j?description=The%20React%20framework%20for%20production&file=app%2Fcart%2Fpage.jsx,app%2Fshop%2Fpage.jsx&title=Next.js%20Starter
idk if that works cause I didn't save it but yea... what a mess.
Really don't get whats so hard about this... fukin React/NextJSyou're not gonna give up are ya
I mean I'm close 🤣
if it would just read the localStorage >.>;;
cause it thinks its on the server and window object ofc does not exists
should I build this in vite to use context api to finish it, then leave our attempt how it is until we find a fix? lol
I mean you do you xD lol
At this point I'm just back to remembering why I think its al lshit
SvelteKit ftw
hahaha
yea, dont tempt me
😂
IF I enjoy BE, you know i'm jumping back on svelte quicker than you can say boo lol
mmm I do see errors in my code but nothing helps if I can't get localStorage read...
whats f'd too is I had it kinda working 😂
so localstorage is the issue huh. best we get rid of it 😄 what about uhh... context?
🤣
not going to solve your issues here
its not localStorage persay its where the code runs as I just said.
You would still want to use localStorage with Context API if you wanted a persitant store.
So unless you were doing a SPA wouldn't matter here
yea I was joking
oh right just use a wrapper cause its boilplatey af xD
Medium
useLocalStorage hook for Next.Js, typed, and SSR friendly
I am learning React and Next.js, and I needed a way to access the localStorage without running into problems with SSR. Hooks to the rescue.
tryna wrap my head around context with their wordy wordyness in this course, quite the brain frazzler, but I get the jist of it.
useLocalStorage
Custom hook that uses local storage to persist state across page reloads.
cause in React you can't just do things without importing a wrapper
fml 🤦♂️
I mean I have to check for browser in my Svelte Store too but >.>;;
This is what you would do with ContextAPI too
probabaly 10x more code though 🤣
just a bunch of words to me
Yea I'm done I'll let someone who actually enjoys NextJS to help you
I tried xD
you gave it your best shot
meh I gave it my best "I hate this" shot 🤣 the answers are there for ya just gotta implement it with the hook wrapper or making your own.
almost be easier to just setup sqlite and use the RSC 😂
I began doing it in vite just to see how it works wit hcontext etc to have a finished product until we can get someone to help, and I already regret starting lmao
well that was the plan wasn't it, lol
learn some backend, come back, jazz it up
yea do that
ill get rid of this vite project, we'll come back to this later if no-one helps in the mean time
Hah.
😉
I use it all
ALL THE THINGS
Localstorage is great for local info
try this
I’ll try this when I’m home in a few hours, thanks for the attempt! 🤞
Alright I tried it, but it didn't fix the problem... nice effort though 😅
what does it get hung on?
I wasn't aware you could do anons like that in the useEffect/state hooks it looked like a interesting solution
this appeared in the terminal, but in dev tools there's no error
but I get this
ah same issue
aye
ya its trying to use localStorage on the server
which is like
window
what?
use client
is a lie? lol items.push()
this feels illegal 😂I mean lol
can't we do
its faster than [newItem, ...items] but I don't konw how that works with React and rerendering if it ignores it.
hmm fair
i mean since u r not allowed to directly change state it just feels so wrong to do 😂
the issue is getting localStorage read to not wig out
thinking its serverside xD
hmm i see
damn i need to get good with react ;-;
I think its horrid, but I don't have to use it lol
i learnt about useState and useEffect and just ran away with my life
Oh, it works in dev mode lol, got to do some extra things to make it run in production due to window and local storage
yeah we figured that, nice attempt though! I think ill probably come back to it once ive learned soem backend and throw that in there instead haha
ill give it another crack in a few, it was a fun challenge, anything to get me off css lol
So the way Next works is it renders components on server and sends rendered HTML to the client. However sometimes we need to use client-side stuff in our components such as localStorage, React hooks etc. In that case we mark component as client component with
"use client"
at the top of the file so Next knows it should be rendered on the client . However client components are initially rendered on server, and then on client. I'm not sure exactly how and why that works but that's why sometimes we need to first make sure we're on client-side using if (typeof window !== 'undefined') {/* ... */}
before doing some client side stuff. We don't need it for hooks though, I guess they have client-side check inside of them or something lol.
@b1mind hope this clears some things out 😂I already understood this
I'm triggered by how stupid it is to have
use-client
and still have to check for window
But I get it cause if it can it will render it out ssr first then client
SvelteKit is very much the same way and* you do have to check if browser there too. >.>;;
We don't need it for hooks though, I guess they have client-side check inside of them or something lol.can you give an example of this cause yea that is what I thought if it was inside a effect/state hook you didn't need to check window object. This seems to work 🤘 well kinda... seems to keep the last added in the array so you get two of it till you come back 🤔
have you cleared localstorage
yea mate
i didn’t seem to get that 🤔 but code is code
facts xD lol
im unhappy with it anyway due to the exhaustive deps warning
😢
seems to do it
im going to try and move the deps into the useeffect but when i try i get loops lol
and still end up needing searchParams
you could use a dynamic route too as I was telling CDL
i’m honestly not familiar with nextjs new apps route thing, still doing it the old way
old way the clientside only way?
or pre folder routing you mean?
uhm, just the structure, src/api, src/pages etc
the latter i suppose
gotcha yea
it is teh way
I don't do React though so I don't have a leg in the race 🤣 if I did I'd probably pick Remix though
i think i ran into problems utilizing Prisma Orm with the new way
ran back to what i knew
anyways, i relearned a lot about useEffect trying to fix this, 🫡
Yea I gotta learn a little bit too, and how much I'd rather still use SvelteKit 🤣
glad you did useEffect, I hate it. Hah
I get the item being added twice with your latest attempt,
update: it seems to work
if you use the edited bit I did you wont
this bit
I am still thinking about if the localstorage set should be done on unmount or not 🤔
I mean it should be a persistent store
but this works for now 😄
yeah if it works :thumbup:
this is what I'd probably use, or make my own.
yeah don't need to go wild on it haha, I really appreciate the efforts!
how do we make it work without exhaustive deps warning 😩
you would think an empty dep array is a react feature so that we can explicitly state we want this effect to run once
sorry what deps warning?
I seem to get this
though weirdly, now whwnever i refresh it adds an item to the cart
i get an exhaustive deps lint warning when building
no me gusta eso amigo 👴🏼