Single Responsibility Principle in React (Design Patterns)

  Рет қаралды 43,322

Cosden Solutions

Cosden Solutions

4 ай бұрын

Join The Discord! → discord.cosdensolutions.io
VSCode Theme | Font → Material Theme Darker | Menlo, Monaco "monospace"
In this video, we talk about the Single Responsibility Principle in React. The Single Responsibility Principle (SRP), is one of the foundational concepts of the SOLID principles. SRP in React means that every component should only have one responsibility and focus on that, while delegating everything else to other components or hooks. In this video, I will show you how to apply the Single Responsibility Principle design pattern in React.

Пікірлер: 144
@ayberk272
@ayberk272 4 ай бұрын
I don't know if this kind of content is actually boring to others but I must say this kinda stuff is what actually helps you become a "good" developer. I can't even stretch the importance of these kind of contents enough. I appreciate the effort, keep it up!
@cosdensolutions
@cosdensolutions 4 ай бұрын
That's what my content is for! Glad you enjoy it! 🤙
@ardianhotii
@ardianhotii 4 ай бұрын
Excatly , I think this too . This is the best react app architecture in my opinion , keep it up!
@ositaka
@ositaka 4 ай бұрын
This is the kind of video you don't find on a react course 🎉
@cosdensolutions
@cosdensolutions 4 ай бұрын
I'm actually making a React course that will have much more of this and in much greater detail and scope, stay tuned!
@binayakpurohit
@binayakpurohit 3 ай бұрын
Hie, can anyone here explain me will it be a good practice to create do many files for one component rendering. i started react a few months ago and now i have a huge project which has 25+ pages. pages become messy and i sm planning to remove the uglyness, can anyone suggest me the best way for it
@riseonelimit
@riseonelimit 4 ай бұрын
There are so many React Tutorials out there but no one shows how to structure the components, Thanks for the great content pal!
@cosdensolutions
@cosdensolutions 4 ай бұрын
will make many more videos for sure. Also working on a full React course that goes way beyond what my videos do, so stay tuned 🤙
@JosephDicdican
@JosephDicdican 2 ай бұрын
💯% helpful, been using react for more than 2 yrs but this makes a lot easier. i'm gonna follow this in our projects. Thx for great tutorial.
@JR-hb6jr
@JR-hb6jr 4 ай бұрын
Moving the useQuery to new file made a huge difference for me. Thank you very much for these kind of video. Really helpful.
@sachintharuka6154
@sachintharuka6154 4 ай бұрын
Because of ur videos my react project code gets better, keep making more videos, so helpful ❤
@martapfahl940
@martapfahl940 4 ай бұрын
This channel is amazing, thank you!
@moonshadowwlb
@moonshadowwlb 4 ай бұрын
I had a light idea of what this was but this video took it to the next level, thanks a lot
@anonysmooth648
@anonysmooth648 4 ай бұрын
never fail to amaze me, thank you for this tutorial
@ekchills6948
@ekchills6948 4 ай бұрын
Very detailed explanation sir. I learned alot. I would start implementing custom hooks more in my applications to separate too much logic from my components
@ddorabang
@ddorabang 3 ай бұрын
어느정도 이해가 됐습니다. 코드가 아주 간결해지네요. 헤메지 않도록 파일 이름을 잘 작성해야겠어요
@Jdthegeek
@Jdthegeek 4 ай бұрын
I wish more of my colleagues (and KZbinrs making tutorials) did this. It always makes it so much easier to work with for me. One issue that I do sometime face doing this though, is with forms etc where a ref is required. Would be useful to have a part 2 that covers these more complex scenarios
@lexsemenenko7044
@lexsemenenko7044 3 ай бұрын
I am here for these kinds of videos. Anything that helps apps scale. Really appreciated. In my scenario example product card may look different in different site areas, so I create extra component props launch as inHeader, in body, inBodyBtm and than render there any additional components I may need to show up an my parent component
@cosdensolutions
@cosdensolutions 3 ай бұрын
I would instead make product card a compound component, and let the parent plug and play the elements it wants in whatever order. Passing props around works, but doesn't scale well at all!
@AndreasFroyland
@AndreasFroyland 4 ай бұрын
Thank you for yet another great video. Really grateful for your content
@mikevillarreal8291
@mikevillarreal8291 4 ай бұрын
Dude, you are doing quite an excellent work with React. You remind me of what ArjanCodes does for python.
@felipecabreira4321
@felipecabreira4321 4 ай бұрын
Amazing video as always dude ! keep going :))) helping me alot
@josuepimentel2488
@josuepimentel2488 Ай бұрын
very clear and precise, well done buddy, thanks and keep it up. Suscribed!
@abdal-fadeelhamdyabdal-fad1649
@abdal-fadeelhamdyabdal-fad1649 4 ай бұрын
Great explanation this series of design patterns will be great
@plfmoura
@plfmoura 4 ай бұрын
Awesome solution!
@IveSpentMonthsPreparingForThis
@IveSpentMonthsPreparingForThis 2 ай бұрын
In addition to styling reasons mentioned by Darius, the at 14:44 is also necessary to render the ProductList component in the event that the products array is empty. Otherwise if the array was empty, you'd get an error because the .tsx file isn't returning a component to render.
@ImranKhan-be8tp
@ImranKhan-be8tp 2 ай бұрын
very well explained. awesome
@stepanostapuk4120
@stepanostapuk4120 4 ай бұрын
I think we have to keep in mind that it is important to keep balance between KISS and Single responsibility principle
@NewtonJR1987
@NewtonJR1987 4 ай бұрын
Amazing content, thank you so much!
@Unlimited_Codes
@Unlimited_Codes 4 ай бұрын
Finally, keep going in this tutorial to complete all solid principles using react.js with typescript
@davidhusted817
@davidhusted817 3 ай бұрын
Thanks so much for the explanation Sir
@imamdroubi6537
@imamdroubi6537 3 ай бұрын
This channel is a treasure, too bad I didn't find it long ago. Thank you for the amazing content.
@tamaniphiri
@tamaniphiri 4 ай бұрын
Clean code, amazing🚀💯
@wagnerfillio1031
@wagnerfillio1031 4 ай бұрын
All the examples we see talk about the list. It would be nice, an example of creating, editing and deleting, along with the save method, using SRP
@TechSpot56
@TechSpot56 4 ай бұрын
Really helpful content. Keep it up.❤
@nawazishali274
@nawazishali274 4 ай бұрын
Nice content and topic to learn ❤
@redwansikder3847
@redwansikder3847 4 ай бұрын
Thank you for this video.
@madehayoussof
@madehayoussof Ай бұрын
Thanks your effort!
@gmjitendra
@gmjitendra 4 ай бұрын
Excellent. thanks
@joeljorge7339
@joeljorge7339 4 ай бұрын
Great tutorial
@jeffwallsinger6411
@jeffwallsinger6411 4 ай бұрын
Very well done
@shakapaker
@shakapaker 4 ай бұрын
great content!
@HeatElectro
@HeatElectro 3 ай бұрын
Thank you so much for this content! I wonder how you would define the layout of the several components in the ProductPage? Inside the component or would you put it somewhere separately?
@dimitridoroshko
@dimitridoroshko 4 ай бұрын
Thanks, man. Want more videos of design patterns in react 😊
@cosdensolutions
@cosdensolutions 4 ай бұрын
they're coming
@Spkntei
@Spkntei 4 ай бұрын
hank you for this video
@BarakerZeonlist
@BarakerZeonlist 3 ай бұрын
very clean
@desentralizandolared9239
@desentralizandolared9239 3 ай бұрын
Amazing, thanks 👍🏽👍🏽⚡⚡⚡⚡
@evandroteixeira5788
@evandroteixeira5788 3 ай бұрын
SRP is only the first step down the road, if you want to become a better developer it's completely worth it to take some time to learn all the principles of SOLID and understand how to apply it efficiently into your projects. Great job with the explanation, maybe you could try the Open/Closed Principle next? Thanks.
@eshginfarzaliyev
@eshginfarzaliyev 4 ай бұрын
Thanks, man
@Tesfamichael.G
@Tesfamichael.G 4 ай бұрын
Excellent
@harag9
@harag9 4 ай бұрын
Thanks for that, just wish all applications was that simple. the one I just took over at work is a nightmare... looks nothing like yours :) Keep up the excellent work!
@cosdensolutions
@cosdensolutions 4 ай бұрын
I know the feeling, I also had to to take over a really bad application once, but slowly we made it look more like this
@Gringo0517
@Gringo0517 4 ай бұрын
Awesome and clear explanations! I was curious about exports. Here u are using default exports but I see a lot of codebases use named exports. When would u choose one over the other?
@cosdensolutions
@cosdensolutions 4 ай бұрын
They're both fine, I'm just used to default exports personally
@shakapaker
@shakapaker 4 ай бұрын
​@cosdensolutions In my experience named exports are easier to refactor cause the import name will change but with default, it can leave the old one which can be confusing and it will not even error
@stanislauyan3204
@stanislauyan3204 4 ай бұрын
Thank you for this video. Your code is really great! It is really important to teach this idea! The value of it is huge and is really important to follow this design principle. However! You cannot apply SOLID to react directly. SOLID was made for OOP. You also violate this rule in your previous video if you use this principle as it was described in SOLID. Single responsibility principle is also not single feature principle. It says - you should have ONE REASON to change. It says nothing about “it does one thing”!!! This is important! I would suggest to never use S in connection to the SOLID in react. In is even more to U in CUPID principles!
@doniaelfouly4142
@doniaelfouly4142 10 күн бұрын
Thanks
@bramreinold
@bramreinold 3 ай бұрын
Hi, well explained mate! I'm wondering if you have also a more advanced example where you do CRUD (refetching data after creating a new item for example).
@cosdensolutions
@cosdensolutions 3 ай бұрын
Not yet, but I will!
@fatehfarooqui3587
@fatehfarooqui3587 3 ай бұрын
Would it be better if we move the conditional logic for showing loading or error or products inside the extracted components? So the productspage will have no conditionals
@cosdensolutions
@cosdensolutions 3 ай бұрын
Nope, it's the responsibility of the products page to decide it wants to show something on loading. The loading component only has the responsibility to show loading UI
@fadfooood
@fadfooood 3 ай бұрын
Is it possible that error state or loading state could render together? Sometimes I need to dig deeper in React Query to know when error is reset.
@LilyDotan
@LilyDotan 4 ай бұрын
What vscode extensions / configurations do you use to the have missing import indication in the code (red underline under what is missing) and when you click on it - it adds the import automatically? (As you do in minute 06:52 in the video) Thanks :)
@cosdensolutions
@cosdensolutions 4 ай бұрын
Have a whole video on my vscode setup!
@user-wy9rj6km6n
@user-wy9rj6km6n 4 ай бұрын
Thank you for the video. Just a small question, what's the structure of unit tests for this example?
@cosdensolutions
@cosdensolutions 4 ай бұрын
depends on what you want to unit test, I usually wouldn't unit test any of these components. I would only unit test UI components and components that are designed to be re-used across many places. In that case I'd colocate the unit test file with the component and write all the tests there. I do most of my testing with e2e integration tests generally
@uzairahmed2975
@uzairahmed2975 3 ай бұрын
Well Explained this help me alot to become a good developer, I have a question the same principle should follow in nextjs?
@cosdensolutions
@cosdensolutions 3 ай бұрын
yes definitely
@anaspk6413
@anaspk6413 3 ай бұрын
"Great content."
@marwajomaa7594
@marwajomaa7594 Ай бұрын
any recommendation for advanced react like books, online websits, etc
@andreaverrecchia3907
@andreaverrecchia3907 4 ай бұрын
In case I need to implement the "enabled" prop for useQuery, how can I do that? Is it enough to pass the 'enabled' prop and simply include it in the useQuery?
@cosdensolutions
@cosdensolutions 4 ай бұрын
you don't need to even include it in the useQuery, just pass it
@ahmedfoda9823
@ahmedfoda9823 4 ай бұрын
thanks for this great video, can you talk about how we can write tests and do automated testing for react code?
@cosdensolutions
@cosdensolutions 4 ай бұрын
I have a video on cypress! But will make more
@ahmedfoda9823
@ahmedfoda9823 3 ай бұрын
thanks@@cosdensolutions
@user-en2lj9xb6u
@user-en2lj9xb6u 3 ай бұрын
which extensions u use in vs code...
@MrJettann
@MrJettann 3 ай бұрын
I should ask you, is it good to just check for products(array) in this way? Because empty array will also return true, I'm usually checks for !!pruducts?.length for that. Am I doing it wrong?)
@cosdensolutions
@cosdensolutions 3 ай бұрын
yeah it's fine, if it's an empty array it will render the ProductList but no products will show. It depends on how you want to structure it
@MrJettann
@MrJettann 3 ай бұрын
@@cosdensolutions yeah, that's true, but there will be empty div inside html, this is not good IMHO
@yurikhunyk2877
@yurikhunyk2877 4 ай бұрын
Greate video! Can you explain in the same way rest of SOLID princeples?)
@balamanohar4946
@balamanohar4946 3 ай бұрын
Is single responsibility principle and modularity both are same ?
@bongbuincyril6150
@bongbuincyril6150 2 ай бұрын
I love u bro
@bongbuincyril6150
@bongbuincyril6150 2 ай бұрын
Keep up
@user-ku2sn1wz1c
@user-ku2sn1wz1c 3 ай бұрын
I would also pass the loading, and error props inside of these components and would do the checking inside of them. But that is a matter of taste. Anyway, things shown in the video distinguish juniors from more experienced guys.
@user-kl9hr5ev1n
@user-kl9hr5ev1n 4 ай бұрын
👍
@Amoz21
@Amoz21 2 ай бұрын
Can you do video for all SOLID Principles applied to react application
@user-ll4qy6cg9i
@user-ll4qy6cg9i 3 ай бұрын
Should we use isError or error in react query?
@cosdensolutions
@cosdensolutions 3 ай бұрын
both, depending on what you need
@user-cm2nz9ik4v
@user-cm2nz9ik4v 4 ай бұрын
Hello Cosden you are seriously a great mentor for me Hope that you remember me can you please suggest me how i can improve this type of code please let me know where i can share the file
@cosdensolutions
@cosdensolutions 4 ай бұрын
post it on the Discord, you'll get answers!
@user-cm2nz9ik4v
@user-cm2nz9ik4v 4 ай бұрын
I have shared the code please check thanks in advance.
@raphael.portela
@raphael.portela 4 ай бұрын
can you do a video on composition pattern with a more real world apporach instead of a simple component showing a message that is shown on the various videos about composition pattern?
@cosdensolutions
@cosdensolutions 4 ай бұрын
you mean component composition? i.e. Select, Select.Option, etc?
@raphael.portela
@raphael.portela 4 ай бұрын
@@cosdensolutions yes!
@abel090713
@abel090713 3 ай бұрын
I feel like you shouldn't be moving things around based on the single responsibility principal, The code at 9:00 was probably the best version of this code. You can clearly tell the output of the UI based on state and you don't have to navigate to different components that only do one thing resulting in you trying to keep multiple pieces in your head, Things living in one place makes it easier to understand and debug (big components are not inherently bad). You should be moving things into their own components based on re-usability and functionality. You would want a loading and error component not because they should only have one responsibility but rather because you want to reuse them several times in your app. And you might want to move the product UI into it's own component if you needed some logic to run per component.
@idiot3641
@idiot3641 4 ай бұрын
Shouldn't we pass the error and loading state as a parameter to those components?
@cosdensolutions
@cosdensolutions 4 ай бұрын
For loading not really, for the error one, you could pass it the error so it displays it
@idiot3641
@idiot3641 4 ай бұрын
@@cosdensolutions 👍
@nanonkay5669
@nanonkay5669 Ай бұрын
This is good, but the single responsibility principle doesn't necessarily mean everything should do just one thing. It could be things that are closely related as well.
@tnnz9920
@tnnz9920 Ай бұрын
sir, could you put results instead of only code on videos? anyway I like ur videos
@Simple_OG
@Simple_OG 4 ай бұрын
Same for next js projects ?
@cosdensolutions
@cosdensolutions 4 ай бұрын
yeah, just no hooks or state in server components but same principles apply
@Simple_OG
@Simple_OG 4 ай бұрын
@@cosdensolutions Thanks i made some changes in my project `import { Notes } from "@prisma/client"; import SingleWobbleBadge from "@/components/Dashboard/Home/SingleWobbleBadge"; import SingleWobbleDescription from "@/components/Dashboard/Home/SingleWobbleDescription"; import SingleWobbleImage from "@/components/Dashboard/Home/SingleWobbleImage"; import SingleWobbleTitle from "@/components/Dashboard/Home/SingleWobbleTitle"; interface SingleWobbleProps { wobble: Notes; } export default function SingleWobble({ wobble }: SingleWobbleProps) { const { category, createdAt, description, imageUrl, title } = wobble; return ( ); } ` what do you think earlier i am rendering all in same component
@mahendrasinhgohil44
@mahendrasinhgohil44 2 ай бұрын
It’s make really hard to debug if nested component there without typescript 😅
@cosdensolutions
@cosdensolutions 2 ай бұрын
everything is harder to debug without typescript 😁
@bl1tzQ
@bl1tzQ 3 ай бұрын
So, do you mean that, if I have 100 pages, I should create 100 custom hooks just to fetch data from API? What about DRY? Why we can’t just create 1 universal hook and use it anywhere?
@cosdensolutions
@cosdensolutions 3 ай бұрын
Well yeah, if you have pages, users, posts, comments, and 100 more entities you should make a hook for each. Although you won't in practice. If you have 100 user pages, one hook is what you need
@birapci
@birapci 3 ай бұрын
You may have 100 pages but also your queries wont be much different. So you can implement an universal-like hook for 'Users' which requires the optional query params (like createdAt-updatedAt-roleId etc.). In every single page you can customise your query.
@user-rt1kv6su5o
@user-rt1kv6su5o 3 ай бұрын
I'm in my 20s learning to be a programmer/coding, is it too late or not? I'm doubtful about my current age of 🙂
@cosdensolutions
@cosdensolutions 3 ай бұрын
you're way early
@mel_alejandrino
@mel_alejandrino 3 ай бұрын
do you have typescript tutorial?
@cosdensolutions
@cosdensolutions 3 ай бұрын
will soon post a JSX to TSX video!
@mel_alejandrino
@mel_alejandrino 3 ай бұрын
@@cosdensolutions thank you!
@jspesh
@jspesh 3 ай бұрын
This is great but can you do it on a 'real' application? Bit more complex?
@cosdensolutions
@cosdensolutions 3 ай бұрын
Yeah, building a whole project in my upcoming course where we do all of this globally and more design patterns
@jspesh
@jspesh 3 ай бұрын
@@cosdensolutions Excellent. Thanks! I love your content, it's been really helpful
@jspesh
@jspesh 3 ай бұрын
@@cosdensolutions Excellent. Thanks! I love your content, it's been really helpful
@rakulagn4598
@rakulagn4598 4 ай бұрын
The Approach is Good Until If you need to Fetch The Product based on Pagination Data Definitely you must place your fetching logics inside the components that's the better way we can't pass the pagination values to hooks everytime right ?
@cosdensolutions
@cosdensolutions 4 ай бұрын
I usually hold the fetching logic in the page component and the ProductList component can just take a callback whenever it reached the end of the list to fetch more products
@2gbeh
@2gbeh 4 ай бұрын
I prefer the product-list should just be part of the page component because the logic won't change or grow overtime. The entire logic is basically a loop and i feel that's too small to be extracted.
@cosdensolutions
@cosdensolutions 4 ай бұрын
So all of my videos are kept super simple right? It will never be just that amount of code. In real projects it's often way more so this pattern applies. Sure if it's a personal small project, you can get away with putting everything in one main component, but I'm not teaching you to build simple projects
@ardianhotii
@ardianhotii 4 ай бұрын
I mean you are kinda right but he's teaching how to write code in the best way possible for scaling and managing apps
@souviksarkar8691
@souviksarkar8691 3 ай бұрын
make a video on zod.
@partiid
@partiid 4 ай бұрын
Very informative video indeed however i don't think this was some sophisticated example. Would highly appraciate some more complicated examples of solid. Anyway, thanks for posting!
@cosdensolutions
@cosdensolutions 4 ай бұрын
I mean, this is for beginners mostly so I keep it simple, but for what it's worth, most of your components should look this simple even in a big app
@DazzyDude00
@DazzyDude00 4 ай бұрын
You'd think experienced devs would use snippets. Not typing out: export default function... everytime 😂
@cosdensolutions
@cosdensolutions 4 ай бұрын
Ok so I will comment on this. I have snippets, however I just made the change from arrow functions to declaring them the old school way and I didn't get to the snippets yet lol. But it is fixed in a couple of videos haha
@davidhusted817
@davidhusted817 3 ай бұрын
Avoid impure functions as we can this is the SRP
@hesoncontroler
@hesoncontroler 4 ай бұрын
I do most of what you're suggesting here. But, I would have suggested that unless your ui components need to be reused, it is actually easier to keep them in the safe file. If you're extracting UI components to separate files and they are not reused anywhere else. That's just silly. And actually harder to understand imo.
@cosdensolutions
@cosdensolutions 4 ай бұрын
It's not silly. I'm teaching how to build complex apps, and in complex apps, you'll always re-use these components so defaulting to this pattern makes sense
@rimha6846
@rimha6846 3 ай бұрын
The SRP does not mean doing one thing
@gerryramosftw
@gerryramosftw Ай бұрын
what does single mean tho
@MathForAll81
@MathForAll81 4 ай бұрын
Hi, we need a playlist for different design pattern implementations in react 🙏🏻
@anonymous_basics
@anonymous_basics 3 ай бұрын
how i can test this components. and is it necessary to make tests for all the components ?
@cosdensolutions
@cosdensolutions 3 ай бұрын
I honestly mostly only do e2e tests of the whole app, and not individual components
@nawazishali274
@nawazishali274 4 ай бұрын
Can I connect with you on social media accounts to see timeline about your latest videos ,twitter ,insta or any other etc...
@cosdensolutions
@cosdensolutions 4 ай бұрын
I'm not on twitter, but you can check me out here, on Instagram, or TikTok!
@nawazishali274
@nawazishali274 4 ай бұрын
@@cosdensolutions Got it !
@anonymous_basics
@anonymous_basics 3 ай бұрын
my team mates doesn't follow even half of this. It is so hard to go through the project. i wish they watch this video 😂
@cosdensolutions
@cosdensolutions 3 ай бұрын
send it to them
Refactoring a React component - Design Patterns
15:19
Cosden Solutions
Рет қаралды 88 М.
Why Did They Do This?! (Code Review)
18:48
Cosden Solutions
Рет қаралды 14 М.
FOUND MONEY 😱 #shorts
00:31
dednahype
Рет қаралды 9 МЛН
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 26 МЛН
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 43 МЛН
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Рет қаралды 168 МЛН
I Was Wrong About Single Responsibility Principle | Prime Reacts
8:14
React Clean Code: Advanced Examples of SOLID Principles
28:03
SOLID Principles: Do You Really Understand Them?
7:04
Alex Hyett
Рет қаралды 114 М.
Learn React Hooks: useMemo - Simply Explained!
13:41
Cosden Solutions
Рет қаралды 73 М.
The problem with useEffect
11:37
Cosden Solutions
Рет қаралды 30 М.
The correct way to optimise React
11:29
Cosden Solutions
Рет қаралды 28 М.
Learn React Hooks: useContext - Simply Explained!
15:46
Cosden Solutions
Рет қаралды 118 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
You Are Using useEffect Wrong
14:40
Cosden Solutions
Рет қаралды 29 М.
FOUND MONEY 😱 #shorts
00:31
dednahype
Рет қаралды 9 МЛН