Refactoring a React component - Design Patterns

  Рет қаралды 97,146

Cosden Solutions

Cosden Solutions

Күн бұрын

Пікірлер: 135
@cosdensolutions
@cosdensolutions Ай бұрын
Hope you enjoyed the video! I also have a really cool and unique course that will teach you way more than this video. You'll learn how to build an actual complex project with React. It's called "Project React" and you can find it at cosden.solutions/project-react. Also, I have a free weekly newsletter called "Import React" with tutorials, news, and cool stuff about React! You can sign up at cosden.solutions/newsletter?s=ytc
@jaortiiss
@jaortiiss 9 ай бұрын
This could be a real scenario of a PR session. A senior dev explaining concepts to a junior one in a real life project. Pure GOLD.
@VineetS-s5x
@VineetS-s5x 6 ай бұрын
Your videos are truly remarkable! I've been on the hunt for quality content like yours for months and finally stumbled upon the right place. My gratitude for your excellent videos
@abrahamibizugbe477
@abrahamibizugbe477 2 ай бұрын
Mehnn.. this is the smartest idea for a SE tutorial content I have seen on YT.. You're doing a great job Man❤️❤️
@kennardsugirotok7374
@kennardsugirotok7374 9 ай бұрын
Please do more of these refactoring. Really helps especially for beginners like me
@fellypsantos_
@fellypsantos_ 9 ай бұрын
I've learned more in 15 min them that in 1 year
@irfansaeedkhan7242
@irfansaeedkhan7242 9 ай бұрын
where were you all this time, i was searching for this practical videos for a long time, thank you so much
@cosdensolutions
@cosdensolutions 9 ай бұрын
well, I'm here now ☺️
@ry902
@ry902 8 ай бұрын
Need more of this . Really helps when you knows the react framwork but don't know how to apply them in actual practical scenarios .
8 ай бұрын
I really like your approach "hooking" the logic when needed. One little thing I would add is using constants in a separated file of same name, for example: const CACHE_INTERVAL = 100 * 60 * 60 const PAGE_VIEW_ANALYTICS_ID = "page_view" And reuse them and even if someday you need to rename / change values, you do it in only one place: 🙂 ... GcTime: CACHE_INTERVAL, staleTime: STALE_INTERVAL ... trackEvent(PAGE_VIEW_ANALYTICS_ID, ...
@joaomarcelofurtadoromero8277
@joaomarcelofurtadoromero8277 9 ай бұрын
Gold content, would love to see more refactoring
@chrismixlist
@chrismixlist 9 ай бұрын
your videos provide information with great value 👌
@excelnutcracker4312
@excelnutcracker4312 8 ай бұрын
I'm new to react and TIL, a whole component re-render when a piece of state change. I never knew why would you want to put the modal open state inside and think like 'I can prevent all of this component to render'. Great and clear explanation!
@NaDennIsGut
@NaDennIsGut 9 ай бұрын
i love your style. just straight to the point and fast delivery of information. please do more refactorings :) subscribed
@cosdensolutions
@cosdensolutions 9 ай бұрын
will do!
@coder_one
@coder_one 9 ай бұрын
I am a bit concerned about writing custom hook with useEffect (last part of video). The IDE can catch quite well if we omit some variables from the useEffect dependency array. By pulling this out into a self-written hook, we completely get rid of support for catching potential errors/missing variables in the dependency array. This can lead to " Reacts one million dollar mistake "...
@andrewbarnes102
@andrewbarnes102 8 ай бұрын
That’s a great point actually. Wonder if there’s any way around it.
@cristhianjhlcom
@cristhianjhlcom 9 ай бұрын
Hello, do you have any video explaining how to organize project folder for big projects? 💪🏽
@prashlovessamosa
@prashlovessamosa 9 ай бұрын
He already created a video.
@fdk.5390
@fdk.5390 9 ай бұрын
@@prashlovessamosa That's what he asked.
@nickwoodward819
@nickwoodward819 9 ай бұрын
Take a look at examples from state management packages like rtk query, they tend to give decent structures
@BSK_Rick
@BSK_Rick 8 ай бұрын
awesome video man! I will share it with my co-workers
@iLLiaGoncharov-d1r
@iLLiaGoncharov-d1r 8 ай бұрын
Using null instead of undefined for explicitly set properties can be a good practice to make the code more explicit and reduce ambiguity.
@MohamedRabi
@MohamedRabi 2 ай бұрын
awesome video! Thank you 👍
@bleakCode
@bleakCode 9 ай бұрын
I really find your videos enjoyable to watch and knowledageable. Keep going!
@a7mdbest15
@a7mdbest15 9 ай бұрын
Loved the video, refactoring is actually what we need to learn
@belkocik
@belkocik 9 ай бұрын
I love these kinds of videos that you explain Design Patterns in React :) Woud love some stuff from React Native too if you can.
@sondernfy
@sondernfy 9 ай бұрын
Love this! Would be nice if you also included the equivalent (or closest approximation) to these in JavaScript as well so we could have a direct comparison. Awesome video
@jech33
@jech33 9 ай бұрын
Hey man I really like your content! I don't know if other people are having the same problem but I think you should turn up the volume on your voice. Somehow when I came from other videos I find myself having to turn up the volume through either my pc or the youtube vid. Keep up the great content! your quality is awesome
@cosdensolutions
@cosdensolutions 9 ай бұрын
interesting, you're the first to have mentioned this. I'm soon getting a whole new setup so this should become much better
@jech33
@jech33 9 ай бұрын
@@cosdensolutions awesome
@daveboo7002
@daveboo7002 9 ай бұрын
It sounds like your mic is on your keyboard?
@HerlonCosta
@HerlonCosta 9 ай бұрын
Amazing explanation, thank you so much!
@rjwhite4424
@rjwhite4424 8 ай бұрын
What is the vs code extension you use for that ts snippet
@cruzlee4092
@cruzlee4092 9 ай бұрын
Great! Thank you for sharing. It's useful for me.
@saqibmuhammad7218
@saqibmuhammad7218 9 ай бұрын
Hats off Bro Really learning lot of new Stuff🥰🥰😍😍
@tomassinkevicius6786
@tomassinkevicius6786 9 ай бұрын
Ideally you only want to send track event when you have a user, but this hook you created will send them even if you don't have one yet.
@cosdensolutions
@cosdensolutions 9 ай бұрын
Yeah but you'd just need to add an if check. In this example thr user was there already
@stn1k3
@stn1k3 8 ай бұрын
Good video, however, `useTrackPageView` should not be typed for specific pages, as it goes against separation in concerns, instead is to be agnostic of the page, so the proper solution would require using generics type PageEvent = { pageId: P; data: D; }; type UserProductsPageEvent = PageEvent; const useTrackPageView = (event: T, deps: DependencyList): void => { useEffect(() => { trackEvent('page_view', event); }, deps); }; // consumption useTrackPageView({ data: { userId }, pageId: 'user_products_page' }, [userId]); lastly correct type for dependency array is DependencyList // defined in react as ReadonlyArray.
@gmjitendra
@gmjitendra 9 ай бұрын
great video, thanks. would appreciate if you also include testing along.
@LinhNguyen-gx5fb
@LinhNguyen-gx5fb 7 ай бұрын
nội dung này tuyệt quá hay anh ơi !
@quanganhinh6252
@quanganhinh6252 9 ай бұрын
Really good video, please do more
@genericbinary558
@genericbinary558 6 ай бұрын
Ohhh nice. So what if I make a typo in the custom hook file where I defined the interface. 😂😂
@LogopedLTD
@LogopedLTD 8 ай бұрын
But eslint react-hook plugin says about warn when put some array of deps from somewhere
@conallen3166
@conallen3166 7 ай бұрын
Hey Cosden, great tutorials, can you tell me how I can create components using rjsfcp? is there a extension needed to support that? ive tried with copilot without luck. Thanks and keep up the good work : -)
@doniaelfouly4142
@doniaelfouly4142 4 ай бұрын
Thanks
@mahdiimad1447
@mahdiimad1447 9 ай бұрын
If you have an useEffect that has a dependency on a prop then you don't need an use|Effect you can only add a key prop to the component, and then it will re-render without using useEffect
@jgagnon88
@jgagnon88 9 ай бұрын
This is a bad advice. If the key changes, the component will unmount and then remount
@teenspirit8714
@teenspirit8714 8 ай бұрын
how about "array.length &&" checking? sometimes it's works wierd
@-mahmoudadel2628
@-mahmoudadel2628 3 ай бұрын
Waht about making a video for event tracker and how to integrate google analytics with react? ❤
@rea_kr
@rea_kr 3 ай бұрын
Could you upload the starter code to your GitHub repository? I'd like to follow along while watching the video.
@mfion1
@mfion1 8 ай бұрын
Nice video
@codexpath2960
@codexpath2960 7 ай бұрын
awesome review
@tony-gx7rt
@tony-gx7rt 8 ай бұрын
can you do some videos on vue js 3 , the resources are scarce and especailly on compositon api.
@marcelp4825
@marcelp4825 8 ай бұрын
Hello ! Why did you choose interface instead type? Great video!
@cosdensolutions
@cosdensolutions 8 ай бұрын
No reason, I just use both interchangeably
@exequiel_hyan
@exequiel_hyan 9 ай бұрын
its a great video, the only thing that i dont think i understood is the deps prop for useEffect... I have a feeling that's going to cause problems in the future, but otherwise, which other options do we have?
@cosdensolutions
@cosdensolutions 9 ай бұрын
That's fair, you could just create a wrapper around the analytics function that's properly typed and use that instead without a custom hook. Might be a better option actually
@shmida1991
@shmida1991 9 ай бұрын
Typescript - work hard to aviod "null of undefined" So much overhead
@aliqureshi4194
@aliqureshi4194 9 ай бұрын
We need a refactoring course 😅
@mustang9951
@mustang9951 9 ай бұрын
quality content
@aytacklnc6642
@aytacklnc6642 8 ай бұрын
3:55 How could you do rjsfcp command? It is not supported by default. There is no information on the internet.
@cosdensolutions
@cosdensolutions 8 ай бұрын
check out my vscode setup video, it's a custom snippet
@ilmanmanarulqori5632
@ilmanmanarulqori5632 9 ай бұрын
lets goooo!! 🎉
@MohammedShibincp
@MohammedShibincp 9 ай бұрын
can you make video in react roadmap pls
@cosdensolutions
@cosdensolutions 9 ай бұрын
yes, it's coming as soon as the new year starts!
@albiceleste101
@albiceleste101 8 ай бұрын
I loved the video! At 7:15 you keep calling the function a "custom hook", wouldn't it require react to be imported to be custom hook?
@thanirmalai
@thanirmalai 9 ай бұрын
Amazing videos
@Quintusflac
@Quintusflac Ай бұрын
I like it
@MrKarl992
@MrKarl992 9 ай бұрын
Thank you. This content is amazing. Do you have some video explaining the file architecture you use? I mean, which folders would you create for this example if it was a real and scalable project?
@cosdensolutions
@cosdensolutions 9 ай бұрын
I have a video on folder structures that goes over this!
@uome2k7
@uome2k7 9 ай бұрын
Isnt the track page hook approach the facade design pattern?
@Kaltste1n
@Kaltste1n 9 ай бұрын
What extensions do you use? Especially those onclick import Thanks!
@wakandagaming5763
@wakandagaming5763 9 ай бұрын
hi can u make playlist about react desain pattren? thanks
@Gangbuster74
@Gangbuster74 9 ай бұрын
Can you please make a video how to append search params or query params into api end point if they are coming from a select input or anything that manages them, currently I just use ternary operator to append them based on their value if it’s present, I dont think this is the best approach, If you could really help showing a practical approach How big projects and companies or experienced developers does that, that would a really great for community thank you
@juozasmas
@juozasmas 9 ай бұрын
Is there any way to automate auto file creation from seelction of jsx? Also for hooks also
@Gringo0517
@Gringo0517 9 ай бұрын
Thanks for the content! I noticed you wrapped the new component in a div when I was expecting an empty tag or fragment. As these aren't exactly equivalent in regards to styles coming from a parent (flex-direction for example), I was curious if this was intentional?
@cosdensolutions
@cosdensolutions 9 ай бұрын
yeah, I added a div there but you could've not added it. I was focused more on the concept and may have added it by habit :D but definitely only add it if you need it!
@Gringo0517
@Gringo0517 9 ай бұрын
@@cosdensolutions thanks for the clarification! Again, thanks for all the great content. In particular the short-form stuff has been great at funneling me back to your longer form information. Liked and subsribed
@onintuverachannel
@onintuverachannel 9 ай бұрын
Please do more . how about color system of the codebase? like you dont need a help of css framework that can produce multiple themes in it.
@ZakTheCreator
@ZakTheCreator 9 ай бұрын
how do i get that 'rjsfc' command that you did?
@aminda6862
@aminda6862 9 ай бұрын
great thanks a lot
@malekimam1444
@malekimam1444 9 ай бұрын
Thank you for the video, I have a question regarding organizing this code block: { products && products.length ? ( ) : ( 'No products available. ' ) } Can we move this logic of checking if there are products inside the component?
@cosdensolutions
@cosdensolutions 9 ай бұрын
yeah you totally can!
@AndreyBelfu
@AndreyBelfu 9 ай бұрын
what vs command do you use for importing files?
@cosdensolutions
@cosdensolutions 9 ай бұрын
Just normal imports from vscode
@lyrical6852
@lyrical6852 9 ай бұрын
Yeah, how to do that import? Whats the shortcut in keyboard?
@mohamedhakem7355
@mohamedhakem7355 8 ай бұрын
@@lyrical6852 Ctrl + space on PC
@HarshVikramSingh-d1b
@HarshVikramSingh-d1b 9 ай бұрын
Hello @cosden I have an issues can you tell me how to fix this react-dom.development.js:67 Warning: Encountered two children with the same key, `.$[object Object]`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted - the behavior is unsupported and could change in a future version. Note: I have given the key but stilll it shows Thanks in advance!
@joseortiz3806
@joseortiz3806 9 ай бұрын
Pls, what is the theme name of the editor?
@cosdensolutions
@cosdensolutions 9 ай бұрын
Vscode
@Becherman
@Becherman 7 ай бұрын
I am not sure that putting the whole modal into button components is a good idea
@abhinavdhama3014
@abhinavdhama3014 9 ай бұрын
Bro can we have one redux think middleware video also?
@akhs001
@akhs001 9 ай бұрын
You need to show how the app works also, before and after refactoring . Not just the code
@ignacioarriagadairiarte4338
@ignacioarriagadairiarte4338 9 ай бұрын
what is your react code snippets extention?
@cosdensolutions
@cosdensolutions 9 ай бұрын
check out the video I made on vscode
@HelenaDuGraus
@HelenaDuGraus 9 ай бұрын
interface or type for components props?
@cosdensolutions
@cosdensolutions 9 ай бұрын
I've switched over to types for everything now
@kost32
@kost32 9 ай бұрын
Thanks for video! What about to make UserProductsFilter component as children of UserProductsFilterButton? No props will be drilled, and reused UserProductsFilterButton component as Modal
@cosdensolutions
@cosdensolutions 9 ай бұрын
Yeah that's actually better! Nice
@einfacherkerl3279
@einfacherkerl3279 9 ай бұрын
the modal and button should not be treated as one component because it violates the single responsibly principal. it has a button that invokes modal and also the modal. the button should start there. only the modal box should b go in separate component. it should take a prop visible that should come from the parent.
@cosdensolutions
@cosdensolutions 9 ай бұрын
Sure, you could also do that. It's debatable though because the whole component would re-render if you did it that way but again, it's not that strict it's just one way of doing things out of many
@einfacherkerl3279
@einfacherkerl3279 9 ай бұрын
@@cosdensolutions React.Memo would be one solution to rerendering. don't re render until props change
@Pilosofia
@Pilosofia 9 ай бұрын
It is called the single responsibility principle not the single element or component principle.
@einfacherkerl3279
@einfacherkerl3279 9 ай бұрын
@@Pilosofia I don't understand what you mean?
@stanislauyan3204
@stanislauyan3204 8 ай бұрын
Single responsibility principle was created for the OOP as part of SOLID principles. In react world the re-rendering should be taken into account. And author is totally correct to make such refactoring. CUPID would fit much better in react, but still you must take into account how react works to make efficient and maintainable code. I think author should not use such name for that part, bcs it confuses a lot.
@DamilolaBada-x3x
@DamilolaBada-x3x 9 ай бұрын
Your videos have always helped me and even though I'm in your Discord channel, I'd still like to ask if you can mentor a junior developer like me. I'd be waiting anxiously for your reply. Thanks
@cosdensolutions
@cosdensolutions 9 ай бұрын
I currently don't do any personal mentoring, but I have these videos and the Discord that really helps! I'm also working on a course that will be extremely helpful as well
@DamilolaBada-x3x
@DamilolaBada-x3x 9 ай бұрын
@@cosdensolutions Thanks for your quick reply. I am a junior fullstack developer and I'm currently tasked at my current job to do a complete rewrite of the app. I'm completely overwhelmed and need guidance on how to build an enterprise and scalable react app. Could you offer me some help or recommendations? Thanks
@khanhduynguyen4726
@khanhduynguyen4726 9 ай бұрын
we need src code :(
@Wakkyguy
@Wakkyguy 9 ай бұрын
Deps [userId] passed to useTrackPageView being a reference type will be changed on every re-render thereby triggering the underlying useEffect of the custom hook again and again. Shouldn't we try to stabilise this reference?
@jamaliseven
@jamaliseven 9 ай бұрын
don't worry, he's a senior and definitely understands react. better to flood your analytics API via beautiful custom hook rather than just wrapping and typing your analytics method (and always enjoyable debugging experience to jump into 100 custom hooks to track down bricked useEffects inside)
@cosdensolutions
@cosdensolutions 9 ай бұрын
Yeah, it would be better to do that actually! I didn't think about it while making the video. You could also create your own function that wraps the analytics one and not bother with extracting the useEffect. It's completely up to you!
@Wakkyguy
@Wakkyguy 9 ай бұрын
@@cosdensolutions sure, thanks
@aleks6331
@aleks6331 9 ай бұрын
No, as useEffect will check items INSIDE of the deps array for consistency, not the array itself. So this code is perfectly fine and won't retrigger unless `userId` changes.
@Wakkyguy
@Wakkyguy 9 ай бұрын
@@aleks6331 Oh, is it? Will check it out sometime.
@xatrox2803
@xatrox2803 9 ай бұрын
What is the extension that you use for the snippet "rjsfcp"?
@cosdensolutions
@cosdensolutions 9 ай бұрын
I made it myself, see my vscode setup vid
@davyengone
@davyengone 9 ай бұрын
At the moment of writing this comment, this video has 1400 views and 11k likes. Something is off with KZbin. Nice video though
@cosdensolutions
@cosdensolutions 9 ай бұрын
Yeah it's a bug, it shows 160 likes and 2.4k on yt studio
@borcio6990
@borcio6990 9 ай бұрын
first x )
@davidezerrilli711
@davidezerrilli711 8 ай бұрын
I'm always a little bit scared of refactoring without setting unit test before refactoring but you know sometimes business times are not dev times
@gixxerblade
@gixxerblade 9 ай бұрын
UserProductsPage will still be rerendered every time the modal shows.
@cosdensolutions
@cosdensolutions 9 ай бұрын
it won't. Child re-renders don't cause parents to re-render
@Shadel.
@Shadel. 7 ай бұрын
Thanks for video but this video is pretty hard for beginners because this topic 2nd concept on react road map. So, Beginners don't know about hooks. I think you can make that basic like this div, h1,img elements. This topic 2nd concept on road map so "Components" but you mentioned that advanced level react informations. I think you should update to video for "How to split larger components into smaller ones (example 1, example 2)" so you can create that new video for beginners because component Layouts are very important in react development.
@nickwoodward819
@nickwoodward819 9 ай бұрын
Only a third through it, but this video is fire. Please do more of these!
@benyamin4634
@benyamin4634 9 ай бұрын
thanks
All 29 Next.js Mistakes Beginners Make
1:45:10
ByteGrad
Рет қаралды 129 М.
Zod Validation in React (Complete Tutorial)
29:44
Cosden Solutions
Рет қаралды 8 М.
Ozoda - Lada (Official Music Video)
06:07
Ozoda
Рет қаралды 10 МЛН
小丑妹妹插队被妈妈教训!#小丑#路飞#家庭#搞笑
00:12
家庭搞笑日记
Рет қаралды 37 МЛН
Bike Vs Tricycle Fast Challenge
00:43
Russo
Рет қаралды 95 МЛН
Single Responsibility Principle in React (Design Patterns)
16:50
Cosden Solutions
Рет қаралды 49 М.
Refactoring a React Component (Design Patterns)
28:20
Cosden Solutions
Рет қаралды 14 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 78 М.
Design patterns in React
14:37
Cosden Solutions
Рет қаралды 165 М.
Why use Type and not Interface in TypeScript
14:12
ByteGrad
Рет қаралды 207 М.
Turning bad React code into senior React code
13:10
Cosden Solutions
Рет қаралды 90 М.
The Most Important Design Pattern in React
35:04
Cosden Solutions
Рет қаралды 52 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,3 МЛН
Ozoda - Lada (Official Music Video)
06:07
Ozoda
Рет қаралды 10 МЛН