STOP using useState, instead put state in URL (in React & Next.js)

  Рет қаралды 228,619

ByteGrad

ByteGrad

Күн бұрын

Пікірлер: 448
@alexnelson2119
@alexnelson2119 Жыл бұрын
A note about replace vs push, the Link component takes a `replace` prop that you can set to true (default is false) if you want the replace behaviour. It also has a `scroll` prop that you should set to false (default is true) if you don't want to scroll to the top of the page each time you click on an option.
@ByteGrad
@ByteGrad Жыл бұрын
Yeah, good points
@EIsenah
@EIsenah 9 ай бұрын
Arigato
@MuhamadAzizPrasetyo
@MuhamadAzizPrasetyo 4 ай бұрын
Thank u!
@samyakpiya
@samyakpiya Жыл бұрын
You are so good at teaching web dev. I love that you show how something can be done with just JS and then proceed to show how a framework like Next.js makes it much simpler! Also, I really appreciate that you cover the edge cases and best practices. I'm learning a lot from watching your videos!
@developerpranav
@developerpranav Жыл бұрын
This is awesome with server components! Very detailed video that includes URL encoding and search params. Waiting for your nextjs course :D
@ByteGrad
@ByteGrad Жыл бұрын
Thanks! 😃
@dimitrisborbotsialos
@dimitrisborbotsialos Жыл бұрын
We’ve actually used this technique 4 years ago on a react application, worked great and still does. We’ve also made a search component to listen for changes on the url and update a context. Our app was complex and many components had to be updated so using a context was the way to go. It’s funny though that no matter what we do we always coming back to 00’s concepts. It’s like php back in the days.
@Diegps
@Diegps Жыл бұрын
it's php with a mint flavor and I'm here for it
@spicynoodle7419
@spicynoodle7419 Жыл бұрын
Modern PHP with HTMX is the dream and I'm living it
@billypentester
@billypentester Жыл бұрын
It depends on the requirements. If you're developing apps that don't share data like admin panels, use states and react. But if you're developing sites that do share data like e-commerce websites, use params and next js.
@webdev_telugu
@webdev_telugu Жыл бұрын
Such a niche concept and he teaches us for free even though he has paid courses. Kudos my man!!
@ByteGrad
@ByteGrad Жыл бұрын
Thanks
@RealRatchet
@RealRatchet Жыл бұрын
It's not a niche concept putting stuff in query parameters was how Web1.0 did things because there was no client state, we've officially come full circle.
@shrin210
@shrin210 Жыл бұрын
​@@RealRatchetNow we write in components and not in pages. That's the only thing changed from Web 1.0
@webdev_telugu
@webdev_telugu Жыл бұрын
@@RealRatchet yeah true, but I'm a new developer so new thing to me
@henriquematias1986
@henriquematias1986 Жыл бұрын
What you mean niche concept? That’s how the internet works and always worked 😊 It’s just that some websites are broken so he’s teaching how to fix it 😅
@lets_see_777
@lets_see_777 Жыл бұрын
somehow your teaching method resonates best with me. There are several popular youtubers on JS but you explain why we are doing things with alternatives. Thank you.
@ByteGrad
@ByteGrad Жыл бұрын
My Professional React & Next.js course is OUT NOW now! Find it here: bytegrad.com/courses/professional-react-nextjs -- this is the #1 resource to master the latest React & Next.js, my absolute best work.
@brancode404
@brancode404 Жыл бұрын
Can't wait for it. I hope it'll be out this Month? 😮
@ByteGrad
@ByteGrad Жыл бұрын
@@brancode404 Sending the exact date to email subscribers :)
@thevasupodcast4561
@thevasupodcast4561 Жыл бұрын
Can you please also club react jobs that you find in your newsletter?
@youneshenni5417
@youneshenni5417 Жыл бұрын
What application will you build in the upcoming next js course? Looking forward to it :)
@ByteGrad
@ByteGrad Жыл бұрын
@@youneshenni5417 I''ll show screenshots of them soon in the newsletter
@wannabe_Akshat
@wannabe_Akshat 4 ай бұрын
Randomly this video popped up in my break session and I thought let's open this and omg I didn't even realized that i needed this for more efficient code. The way you explained it never felt like someone is teaching, i just watched whole video and learned without any hesitations. You are just a piece of art!
@saadelfadil
@saadelfadil 11 ай бұрын
I stumbled upon your KZbin channel just today and had to reach out immediately to express my gratitude for the incredible content you’re sharing. Even though it's only been a day, I've already spent hours soaking in your insights. The way you explain concepts is nothing short of amazing 🔥🔥
@foreverl01
@foreverl01 Жыл бұрын
I found your videos last week and I've seen more than 10 hours of your content. The way you explain things is amazing. Thanks, Wesley! I'm making an e-commerce myself to practice react and next.js, using app router. This solution to avoid using state and making components CC is great! I can't wait for the next js course!! 🥳
@ByteGrad
@ByteGrad Жыл бұрын
Awesome, thank you!
@kevin-ty7it
@kevin-ty7it Жыл бұрын
I have a short attention span, but you shared information continuously, which kept me engaged throughout.
@coffeefps
@coffeefps Жыл бұрын
This is actually clean and good. Just need to handle the magic strings gracefully and it's perfect. You earned my sub.
@silentlyow
@silentlyow Жыл бұрын
I recently started a project on a brand new framework recently (Next.js) with TypeScript as well (first typescript experience as well) and discovered this method of handling something like "state" in server components. Basically I had a product page and needed to do pagination and filtering, I did it using the query string.
@codewithguillaume
@codewithguillaume Жыл бұрын
Huge fan of this approach :)
@simoncunningham2964
@simoncunningham2964 8 ай бұрын
Thank you for this video - I've watched a lot of tutorials lately that go down the route of useState and useEffect but they never felt like the best way - glad I came across this video!
@ebratz
@ebratz Жыл бұрын
This tip saved me a lot of time today! Thank you so much! Eager waiting for your Next.js course!
@vikasni95
@vikasni95 Жыл бұрын
Im very lucky to find your channel on random search.. Tq god for suggesting this gem channel... Tqss dude keep adding more videos 😍👍
@MOURADSLIM
@MOURADSLIM Жыл бұрын
I'm doing a search function and was gonna use a state manager to manage this problem, perfect timing XD Thank you
@Olga-id1qy
@Olga-id1qy Жыл бұрын
Beautiful, it made me smile for a sec as I would write it exactly as you mentioned at the beginning via useEffect(). Now while transitioning to Next.js, I like definitely this one-way approach
@akuya-ekorot
@akuya-ekorot Жыл бұрын
Thanks for the video. I heard of this and needed to see an example of how to go about it and this went above and beyond with even the pitfalls to watch out for.
@rubelhossain8517
@rubelhossain8517 Жыл бұрын
amazing content ! Keep up the good work sir
@ByteGrad
@ByteGrad Жыл бұрын
Thanks, will do!
@LePhenixGD
@LePhenixGD Жыл бұрын
Amazing tutorial man! Never had that idea to use URL parameters not for a search but for products until now, thank you!
@maisamafshar
@maisamafshar Жыл бұрын
Excellent presentation and explanation. Loved the pace of the video, not slow, not fast. Subscribed.🤩
@James-rd1sb
@James-rd1sb Жыл бұрын
I never thought of it like this. I'm sure it'll be useful to know in the future! Many thanks
@veedjohnson
@veedjohnson Жыл бұрын
Definitely one of the best out there!
@andreilucasgoncalves1416
@andreilucasgoncalves1416 Жыл бұрын
Query strings are usually a lot harder to work with than useState, you have to validade the input to avoid errors, specially if you change things and the query data becames stale, but anyways query strings provides a great UX
@zettca
@zettca Жыл бұрын
If your data/endpoint inferred from the searchParams, it won't become stale. searchParams should be the SSOT (single source of truth). And as these are user-controlled, there should be some validation - yes. A small validation function is the trade-off for the greater UX
@zettca
@zettca Жыл бұрын
Another thing: we shouldn't be building the searchParams by hand anyways. There's URLSearchParams for that
@Kaioin
@Kaioin Жыл бұрын
You should be validating data that you use with useState, too, if it comes from userland.
@wisdomelue
@wisdomelue Жыл бұрын
everything has its tradeoffs
@CottidaeSEA
@CottidaeSEA Жыл бұрын
Validation is always an issue and honestly, in a case like this it is simply absurd to expect query parameters to exist immediately. For these things you should always have a fallback.
@pi3ni0
@pi3ni0 Жыл бұрын
Thanks, very good content. A lot of real world cases, can't wait for your course! :)
@taicheeeee
@taicheeeee Жыл бұрын
Best youtubers for beginngers: Lama Dev, and Net Ninja Best youtuber for junior and middle developers: YOU
@ByteGrad
@ByteGrad Жыл бұрын
Haha thanks
@saitharunreddya2046
@saitharunreddya2046 Жыл бұрын
An awesome video with great details and explanation, loved it!!! Keep Up the good work
@romeorel1679
@romeorel1679 Жыл бұрын
Never knew you have had a KZbin channel. I discovered you from your CSS udemy course. Quality work.
@ardianhotii
@ardianhotii Жыл бұрын
This video was very helpful, I'm learning so many new things from you that I didn't know that can cause problems in my apps. Thank you so much
@Lapatate-s1l
@Lapatate-s1l 4 ай бұрын
The best js teacher 👌🏽 not a lot of blabla . Straightforward to the important features . I hope you make udemy courses one day
@jou3883
@jou3883 Жыл бұрын
I love you. You’re an awesome teacher. I’m grateful to have found your channel 🙏🏻
@abdullah_siddiquii
@abdullah_siddiquii Жыл бұрын
The content was amazing. I used this approach in my project.
@user-ge2vc3rl1n
@user-ge2vc3rl1n Жыл бұрын
This is a very cool and unique example, looking forward to more. GJ
@perraanal
@perraanal Жыл бұрын
this guy is the best tech youtuber ever
@arinium
@arinium 10 ай бұрын
You make absolutely great tutorials, thank you for sharing!
@Scrappycoco
@Scrappycoco Жыл бұрын
Great content man. Keep it up!
@mohammadshariarparvez669
@mohammadshariarparvez669 Жыл бұрын
watching your first video , became a fan of you broooooooo.. god bless you
@nomadshiba
@nomadshiba Жыл бұрын
not using react or next, or tsx but this is so true! especially for modals, i like to keep my active modal info on the hash part of the url that way both page data and modal data can be placed on the url but of course you can use url params too i mean you realize that your site/app needs this the moment you refresh the page
@weebdeveloper
@weebdeveloper Жыл бұрын
True, derived state makes some features so much simpler to build, thanks for sharing
@NOTHING-en2ue
@NOTHING-en2ue Жыл бұрын
very well tutorial, you finally teach me how to think the next-js way ❤
@guilhermeferreirabr
@guilhermeferreirabr Жыл бұрын
Dziękuję bardzo za tutoriale, właśnie kupiłem dwa twoje kursy, niesamowita treść! 🚀👏🎬
@ByteGrad
@ByteGrad Жыл бұрын
Thanks Guilherme! Appreciate it
@mu_yaser
@mu_yaser Жыл бұрын
wanted to use this concept in my latest next project and you just explained things i needed to know, good info and explanation
@OpenCoreX
@OpenCoreX Жыл бұрын
This is just amazing. Glad I discovered you - amazing teacher. #subscribed
@perkoz27
@perkoz27 Жыл бұрын
1:59 You state that this is good for SEO, but I've met experts who say the exact opposite: "search engines cannot parse url params and treat every variant as page duplication". Can you provide the source of your claim? I just wanted to read some more about this topic and any additional data to prove to SEO masters that good UX is also good practice for search optimization.
@ZacharyBetz
@ZacharyBetz Жыл бұрын
In case you weren’t aware, your content is top notch 👌
@ByteGrad
@ByteGrad Жыл бұрын
Thanks Zachary, always good to hear :D
@oidualx
@oidualx Жыл бұрын
Thank you so much! This way of handling state is nothing knew, but I personally never put much thought into it until this video, and always defaulted to client state, with all the issues you listed. Typical aha moment. From now on you convinced me to always default to query params to handle state, unless there is a serious reason not to do so.
@igboanugwocollins4452
@igboanugwocollins4452 Жыл бұрын
Neat approach, you just gained another sub
@raymondmichael4987
@raymondmichael4987 Жыл бұрын
I had similar use case today, pulled my hair for hours; Thanks brother 😊
@DiogoLScarmagnani
@DiogoLScarmagnani Жыл бұрын
Wow! I didn't know about History API and its pushState method. Great! It can be used even in vanilla JavaScript projects. Thank you for the video.
@ChrisHaupt
@ChrisHaupt Жыл бұрын
That's a really cool trick using the Link component to append a query param. Didn't know that was a thing!
@billypentester
@billypentester Жыл бұрын
It's really something great for me, actually, I'm looking for something similar to this concept and I got it. So much thanks sir
@kings-code6675
@kings-code6675 3 ай бұрын
Great tutorial. There are situations where useState still shines better and there are situations where this method would be perfect. I remember doing same thing on a client's project earlier this year. I needed to keep the state on the url since I would be navigating back to that same page after user completed their stripe integration. Storing in useState didn't make sense to me, so I stored the state on the url and it worked perfectly. If you're dealing with object states, useState remains the best. If you do not need to persist the state when you share the url, useState is still the best.
@ganumba11
@ganumba11 Жыл бұрын
amazing content from you like always 🔥🔥
@youssefaserrar2001
@youssefaserrar2001 Жыл бұрын
You way of explaining is Awesome, I love you videos.
@ByteGrad
@ByteGrad Жыл бұрын
Thank you so much 😀
@abdulazizaskaraliev6119
@abdulazizaskaraliev6119 Жыл бұрын
yes, I recently had a problem keeping state with ssr and client at the same time. It was pain in the ass, I moved it to url and it feels so good, and at the same time, url is sharable, so your state is not only in your app but you can even share it to someone and they'll immediately see the thing you want them to see. I was thinking what could be the down sides of doing this, i mean my state is not big and only about 1-3 query params most of the time? What could go bad with this ? And I just started to watch the video, and OMG the intro, he talks about all the pain points I had. This is gonna be enjoyable watch, I can smell it.
@alirezagarshasbi6557
@alirezagarshasbi6557 Жыл бұрын
i was looking to make a seperate context for a boolean value as a side effect of another context state change, but with the abuse of url its free state across the app😁 thank you
@prajwalchoudhary4824
@prajwalchoudhary4824 Жыл бұрын
I learned this when I was working with react router lifting state up to the url so even when user navigates around in app we can pass on this state so when they come back to same page the state doesn't get lost
@mohammedelmoutaouakkil
@mohammedelmoutaouakkil Жыл бұрын
Great tutorial! you got a subscriber
@bangmekk10
@bangmekk10 Жыл бұрын
Great video, but how would this work if you have a search component to search something, and want to handle and display the loading state while its being fetched? a solution i could think of is using ternary but what if I wanted to use the next.js default loading.tsx file?
@ish1k4r4
@ish1k4r4 Жыл бұрын
hey, could you please elaborate about the ternary approach? i'm struggling to find a solution for this problem myself 😅😅
@bklynpeter
@bklynpeter Жыл бұрын
Another amazing video. I do have a follow-up question: In your example, the page is a presumably dynamic route? How do we utilize those dynamic elements (again, in your example, 'product' and 't-shirt') at the same time we are accessing the query-params? Thanks as always!
@Midnight_Studying
@Midnight_Studying 7 ай бұрын
a very intense video ,thank you very much teacher.
@septiantonrezkirahmatulloh3837
@septiantonrezkirahmatulloh3837 Жыл бұрын
thank you, sir. from your knowledge. I am learned something new from this.
@spacecowboyofficial
@spacecowboyofficial Жыл бұрын
This just feels right. UX is more than just visuals when designing a website.
@LearnFrontendNow
@LearnFrontendNow Жыл бұрын
Always a super useful technique especially for UI's that you likely want to remember state when you copy and paste the link.
@giandenorte
@giandenorte Жыл бұрын
Just subscribed, I love your content man! I wish the audio can be improve soon.
@ByteGrad
@ByteGrad Жыл бұрын
Yes, will improve soon. 2-3 more videos with this haha
@s8x.
@s8x. 3 ай бұрын
wow fantastic video. learned so much from this
@KamilDrozdz
@KamilDrozdz Жыл бұрын
great content, I was surprised when I saw the browser in Polish hah :D greetings
@TitoMitto
@TitoMitto Жыл бұрын
Your tutorials are very informative
@chrishanthacosta4093
@chrishanthacosta4093 Жыл бұрын
I find Wesley to be the best NextJS teacher.
@nick-ui
@nick-ui Жыл бұрын
Hey! I really glad to see that someone show this method to handling state in url, but this solution has potential disadvantage. It's animations. It's harder control when they're removed from the React tree. So when I use Framer motion, I can't make nice animations. Maybe vanilla css will, idk
@ByteGrad
@ByteGrad Жыл бұрын
Interesting, thanks for sharing
@glauberbispo8922
@glauberbispo8922 Жыл бұрын
mind blowing I always work with states, i'm looking foward to implement this solution in some of my works *-* and it works really good together with server side in next
@Imjoshnewton
@Imjoshnewton Жыл бұрын
Love this solution for handling state especially when you want to set state down the tree and read it across other components. The issue I’ve run in Next 13 is setting the paean jumps you to the top of the page. Even when using replace…
@ByteGrad
@ByteGrad Жыл бұрын
You can configure that on the Link component (set ‘scroll’ prop to false)
@rinyato
@rinyato Жыл бұрын
In the color button section, if u want to uppercase the first letter, u can do it with css, so in tailwind u can use "uppercase" class name. Sorry if im misunderstanding ur intention.
@ByteGrad
@ByteGrad Жыл бұрын
Yeah, it was a silly mistake
@atthanh882
@atthanh882 Жыл бұрын
capitalize
@lorsothy6263
@lorsothy6263 Жыл бұрын
Thank you so much. Now I found a way to select filters without use client.
@mmaarafat
@mmaarafat Жыл бұрын
thanks, mate. it was great explanation :)
@lucasschoenardie123
@lucasschoenardie123 Жыл бұрын
How would you handle the query params if they are dynamic? I mean, on this case you know that you have color and size. What if you got the product options from an api and you don't know what and how many they are. How would you go about generating the search params that you pass to the Link's href?
@blazejmojak
@blazejmojak 8 ай бұрын
good question. I will test this solution, that should fetch data and then set to the URL.
@ota-ke
@ota-ke Жыл бұрын
Pretty well explained. I didn't know server components get the searchParams as props by default!
@jerryfrancis7495
@jerryfrancis7495 Жыл бұрын
Am seriously anticipating your next js and react course. Great content as always
@ByteGrad
@ByteGrad Жыл бұрын
Great to hear Jerry!
@stephenngeorge
@stephenngeorge Жыл бұрын
This is a really great video :) and super interesting. I know it's nothing to do with the video but the "JavaScript magic" to make the first letter uppercase on those buttons is just "text-transform: capitalize;" in CSS 👍
@redeemr
@redeemr Жыл бұрын
or since he's using tailwind just className="capitalize"
@hojdog
@hojdog Жыл бұрын
Sounds like bad UX to force the user to go back through query params in the history, since it’s not what the user expects, but I like the idea of using query strings for the other reasons. So I would just do it the way you did before you used , and turn it into its own reusable hook.
@i.j.5513
@i.j.5513 Жыл бұрын
Very nicely explained and demonstrated 👍
@filipniklas
@filipniklas Жыл бұрын
Great video! Super helpful and well presented!
@ogbillity
@ogbillity Жыл бұрын
I cant thank you enough for this tutorial.
@404-not-found-service
@404-not-found-service 9 ай бұрын
Thanks for the video, I liked it a lot, will you by any chance upload more material like this also using useOptimistic and so on? all the best
@marchugans
@marchugans Жыл бұрын
Great approach
@ByteGrad
@ByteGrad Жыл бұрын
I agree, underused by many devs
@nascob
@nascob Жыл бұрын
It's a great tutorial. Nice pros and good pacing with good examples. A bit of constructive feedback - I think you're missing several important points. 1) Validation - treating the URL as a single source of truth is fine - however it's super easy to mangle, incorrectly enter or purposefully break the URL - would have loved to see some patterns to deal with that. (as a side note - putting `as string` is not asserting anything - it's just telling typescript to ignore the fact that the URL param can actually be an array type) 2) Async examples - in your case - setting the attributes for the t-shirt (esp. on the server side) is straightforward. However if you have a more complicated example where the product details are being retrieved from a database - it gets tricky to validate and apply the URL params against a dynamically set object. Also need to handle resyncing the URL if they are invalid. While the pros are good - there are some cons to this that become obvious with advanced usage.
@ByteGrad
@ByteGrad Жыл бұрын
Yes, great points, thanks for sharing!
@AndersonSousa33
@AndersonSousa33 Жыл бұрын
Your videos are awesome! Just need to raise the audio volume a little bit.
@johncaballero5463
@johncaballero5463 Жыл бұрын
Exactly what I need right now.
@lukas.webdev
@lukas.webdev Жыл бұрын
Great video!! Thanks for sharing. 😉🔥
@ByteGrad
@ByteGrad Жыл бұрын
Thanks for watching!
@guyrandalf711
@guyrandalf711 9 күн бұрын
How I have always wanted to understand this concept Thanks a lot
@wayneswildworld
@wayneswildworld Жыл бұрын
Will you use router.replace instead of the next Link component? In a use case where you don't want to rewrite the URL history
@ByteGrad
@ByteGrad Жыл бұрын
Both are possible. The Link component in Next.js also has a “replace” prop that allows you to configure this
@wayneswildworld
@wayneswildworld Жыл бұрын
@@ByteGrad thanks for the reply. I really like your last few videos I've seen from you on NextJS and tailwind +1 Sub
@WojciechBlachut
@WojciechBlachut Жыл бұрын
That is an excellent video! Thanks so much.
@peterabouabsi8427
@peterabouabsi8427 Жыл бұрын
Depends on your case. This is a great practice for searching and fetching tasks.
@BK-qx3qf
@BK-qx3qf Жыл бұрын
You got subscriber. Nice tutorial.
@mohammadmo9290
@mohammadmo9290 Жыл бұрын
Amazing, thanks mate 👌👌
@ronilbiswas
@ronilbiswas Жыл бұрын
I wonder if Link component is the appropriate one here instead of a button, if seen from an accessibility perspective.
@ByteGrad
@ByteGrad Жыл бұрын
Yeah that’s debatable
@matowang
@matowang Жыл бұрын
One of the problems with using this method on Next.js is that the UI does not update until page.tsx is finished rendered. So if you click "White", it will not update the UI until the client finishes fetching the next page. It will be better to maintain some client state and listen to updates so the client UI can update immediately. Let me know if I'm missing something.
@alexkey9372
@alexkey9372 Жыл бұрын
Great video! but what if the use case is to select something and change the api response? e.g a filter page. don't you need useEffect for that? you will need the filter params first from the actual api and then when url changes it needs to re-fetch the actual response from the api with the new variables via rest or graphql. like fetching products with an array of size selections and an array of colour selections. where the fetch function will need to be?
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 147 М.
Build a Live Streaming App using Reactjs and ZEGOCLOUD
48:55
JavaScript Unleashed
Рет қаралды 95
Air Sigma Girl #sigma
0:32
Jin and Hattie
Рет қаралды 45 МЛН
Ozoda - Alamlar (Official Video 2023)
6:22
Ozoda Official
Рет қаралды 10 МЛН
$1 vs $500,000 Plane Ticket!
12:20
MrBeast
Рет қаралды 122 МЛН
All 17 React Best Practices (IMPORTANT!)
1:46:11
ByteGrad
Рет қаралды 223 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 238 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 505 М.
Why I don't use React-Query and tRPC in Next.js
18:58
ByteGrad
Рет қаралды 101 М.
Speed Up Your React Apps With Code Splitting
16:50
Web Dev Simplified
Рет қаралды 398 М.
URL Params as State in React (Complete Tutorial)
20:33
Cosden Solutions
Рет қаралды 19 М.
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 142 М.
Best of CES 2025
14:50
The Verge
Рет қаралды 198 М.
Air Sigma Girl #sigma
0:32
Jin and Hattie
Рет қаралды 45 МЛН