Single Responsibility Principle in React (Design Patterns)

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 149

  • @ayberk272
    @ayberk272 Рік тому +90

    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  Рік тому +11

      That's what my content is for! Glad you enjoy it! 🤙

    • @ardianhotii
      @ardianhotii Рік тому +3

      Excatly , I think this too . This is the best react app architecture in my opinion , keep it up!

    • @ositaka
      @ositaka Рік тому +1

      This is the kind of video you don't find on a react course 🎉

    • @cosdensolutions
      @cosdensolutions  Рік тому +2

      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 Рік тому

      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 Рік тому +30

    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  Рік тому

      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 🤙

  • @ayanupadhaya944
    @ayanupadhaya944 Місяць тому

    Your tutorials are easier to understand and clear to the point, thank you so much for your valuable guidance....

  • @IveSpentMonthsPreparingForThis
    @IveSpentMonthsPreparingForThis 10 місяців тому

    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.

  • @JosephDicdican
    @JosephDicdican 10 місяців тому +1

    💯% 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.

  • @moatazali1462
    @moatazali1462 7 місяців тому

    Although I've only been following you for a short time, I find what you do amazing. I love the way you explain things so clearly. You are truly great. Thank you!

  • @lexsemenenko
    @lexsemenenko Рік тому +2

    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  Рік тому +2

      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!

  • @JR-hb6jr
    @JR-hb6jr Рік тому +1

    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 Рік тому +5

    Because of ur videos my react project code gets better, keep making more videos, so helpful ❤

  • @JohnBuildWebsites
    @JohnBuildWebsites Рік тому +2

    I wish more of my colleagues (and UA-camrs 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

  • @moonshadowwlb
    @moonshadowwlb Рік тому

    I had a light idea of what this was but this video took it to the next level, thanks a lot

  • @ekchills6948
    @ekchills6948 Рік тому +2

    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

  • @anonysmooth648
    @anonysmooth648 Рік тому +1

    never fail to amaze me, thank you for this tutorial

  • @mikevillarreal8291
    @mikevillarreal8291 Рік тому +1

    Dude, you are doing quite an excellent work with React.
    You remind me of what ArjanCodes does for python.

  • @st0maklija
    @st0maklija 8 місяців тому

    00:04 The single responsibility principle emphasizes that a component should have one and only one reason to change.
    02:25 Single responsibility principle ensures code maintainability and bug identification
    04:30 ProductsPage component is violating the single responsibility principle
    06:40 Creating a custom hook for fetching products in React
    08:39 Implement single responsibility principle in React by delegating specific tasks to separate components.
    10:52 Delegate UI logic to separate components for reusability
    12:54 Implementing Single Responsibility Principle in React
    15:10 Separating responsibilities for components in React

  • @martapfahl940
    @martapfahl940 Рік тому +2

    This channel is amazing, thank you!

  • @imamdroubi6537
    @imamdroubi6537 Рік тому

    This channel is a treasure, too bad I didn't find it long ago.
    Thank you for the amazing content.

  • @ddorabang
    @ddorabang Рік тому

    어느정도 이해가 됐습니다. 코드가 아주 간결해지네요. 헤메지 않도록 파일 이름을 잘 작성해야겠어요

  • @stepanostapuk4120
    @stepanostapuk4120 Рік тому +8

    I think we have to keep in mind that it is important to keep balance between KISS and Single responsibility principle

  • @abdal-fadeelhamdyabdal-fad1649

    Great explanation this series of design patterns will be great

  • @Unlimited_Codes
    @Unlimited_Codes Рік тому

    Finally, keep going in this tutorial to complete all solid principles using react.js with typescript

  • @wagnerfillio1031
    @wagnerfillio1031 Рік тому +1

    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

  • @josuepimentel2488
    @josuepimentel2488 9 місяців тому

    very clear and precise, well done buddy, thanks and keep it up. Suscribed!

  • @AndreasFroyland
    @AndreasFroyland Рік тому

    Thank you for yet another great video. Really grateful for your content

  • @plfmoura
    @plfmoura Рік тому +2

    Awesome solution!

  • @felipecabreira4321
    @felipecabreira4321 Рік тому

    Amazing video as always dude ! keep going :))) helping me alot

  • @evandroteixeira5788
    @evandroteixeira5788 Рік тому

    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.

  • @TechSpot56
    @TechSpot56 Рік тому

    Really helpful content. Keep it up.❤

  • @harag9
    @harag9 Рік тому

    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  Рік тому +1

      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

  • @dimitridoroshko
    @dimitridoroshko Рік тому

    Thanks, man. Want more videos of design patterns in react 😊

  • @davidhusted817
    @davidhusted817 Рік тому

    Thanks so much for the explanation Sir

  • @ImranKhan-be8tp
    @ImranKhan-be8tp 11 місяців тому

    very well explained. awesome

  • @shakapaker
    @shakapaker Рік тому +1

    great content!

  • @NewtonJR1987
    @NewtonJR1987 Рік тому

    Amazing content, thank you so much!

  • @Jesus901
    @Jesus901 5 місяців тому

    love it! ❤

  • @stanislauyan3204
    @stanislauyan3204 Рік тому +2

    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!

  • @bramreinold
    @bramreinold 11 місяців тому +1

    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).

  • @tamaniphiri
    @tamaniphiri Рік тому

    Clean code, amazing🚀💯

  • @madehayoussof
    @madehayoussof 10 місяців тому

    Thanks your effort!

  • @redwansikder3847
    @redwansikder3847 Рік тому

    Thank you for this video.

  • @nawazishali274
    @nawazishali274 Рік тому

    Nice content and topic to learn ❤

  • @BarakerZeonlist
    @BarakerZeonlist 11 місяців тому

    very clean

  • @Gringo0517
    @Gringo0517 Рік тому +2

    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  Рік тому +1

      They're both fine, I'm just used to default exports personally

    • @shakapaker
      @shakapaker Рік тому

      ​@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

  • @jeffwallsinger6411
    @jeffwallsinger6411 Рік тому

    Very well done

  • @joeljorge7339
    @joeljorge7339 Рік тому

    Great tutorial

  • @fatehfarooqui3587
    @fatehfarooqui3587 Рік тому +1

    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  Рік тому +1

      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

  • @uzairahmed2975
    @uzairahmed2975 11 місяців тому +1

    Well Explained this help me alot to become a good developer, I have a question the same principle should follow in nextjs?

  • @emmanuelareiza9699
    @emmanuelareiza9699 7 місяців тому

    TY From Colombia

  • @ВладиславВитковский-п8н

    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.

  • @Spkntei
    @Spkntei Рік тому

    hank you for this video

  • @HeatElectro
    @HeatElectro 11 місяців тому

    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?

  • @Tesfamichael.G
    @Tesfamichael.G Рік тому

    Excellent

  • @olatunjiolakunle6908
    @olatunjiolakunle6908 6 місяців тому

    Would an higher order component make sense in order to manage the loading, error and display component since on a single page you can have multiple request for different resource ?

  • @g-ytub
    @g-ytub Рік тому

    Excellent. thanks

  • @desentralizandolared9239
    @desentralizandolared9239 11 місяців тому

    Amazing, thanks 👍🏽👍🏽⚡⚡⚡⚡

  • @ahmedfoda9823
    @ahmedfoda9823 Рік тому +1

    thanks for this great video, can you talk about how we can write tests and do automated testing for react code?

  • @eshginfarzaliyev
    @eshginfarzaliyev Рік тому

    Thanks, man

  • @abel090713
    @abel090713 11 місяців тому

    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.

  • @LilyDotan
    @LilyDotan Рік тому

    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 :)

  • @fadfooood
    @fadfooood 11 місяців тому

    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.

  • @marwajomaa7594
    @marwajomaa7594 9 місяців тому

    any recommendation for advanced react like books, online websits, etc

  • @yurikhunyk2877
    @yurikhunyk2877 Рік тому

    Greate video!
    Can you explain in the same way rest of SOLID princeples?)

  • @anaspk6413
    @anaspk6413 Рік тому

    "Great content."

  • @OleksiiBurachyk
    @OleksiiBurachyk Рік тому

    Thank you for the video. Just a small question, what's the structure of unit tests for this example?

    • @cosdensolutions
      @cosdensolutions  Рік тому

      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

  • @stevejohnny1111
    @stevejohnny1111 11 місяців тому +1

    This is great but can you do it on a 'real' application? Bit more complex?

    • @cosdensolutions
      @cosdensolutions  11 місяців тому +1

      Yeah, building a whole project in my upcoming course where we do all of this globally and more design patterns

    • @stevejohnny1111
      @stevejohnny1111 11 місяців тому

      @@cosdensolutions Excellent. Thanks! I love your content, it's been really helpful

    • @stevejohnny1111
      @stevejohnny1111 11 місяців тому

      @@cosdensolutions Excellent. Thanks! I love your content, it's been really helpful

  • @raphael.portela
    @raphael.portela Рік тому

    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?

  • @Amoz21
    @Amoz21 11 місяців тому

    Can you do video for all SOLID Principles applied to react application

  • @doniaelfouly4142
    @doniaelfouly4142 8 місяців тому

    Thanks

  • @MrJettann
    @MrJettann Рік тому

    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  Рік тому

      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 Рік тому

      @@cosdensolutions yeah, that's true, but there will be empty div inside html, this is not good IMHO

  • @mahendrasinhgohil44
    @mahendrasinhgohil44 11 місяців тому

    It’s make really hard to debug if nested component there without typescript 😅

    • @cosdensolutions
      @cosdensolutions  11 місяців тому

      everything is harder to debug without typescript 😁

  • @balamanohar4946
    @balamanohar4946 Рік тому

    Is single responsibility principle and modularity both are same ?

  • @andreaverrecchia3907
    @andreaverrecchia3907 Рік тому

    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  Рік тому +1

      you don't need to even include it in the useQuery, just pass it

  • @FredrikStrömberg-q5s
    @FredrikStrömberg-q5s Рік тому

    Should we use isError or error in react query?

  • @Simple_OG
    @Simple_OG Рік тому

    Same for next js projects ?

    • @cosdensolutions
      @cosdensolutions  Рік тому

      yeah, just no hooks or state in server components but same principles apply

    • @Simple_OG
      @Simple_OG Рік тому

      @@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

  • @bl1tzQ
    @bl1tzQ Рік тому

    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  Рік тому

      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 11 місяців тому

      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.

  • @olraclem
    @olraclem Рік тому

    do you have typescript tutorial?

  • @rakulagn4598
    @rakulagn4598 Рік тому

    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  Рік тому +2

      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

  • @tnnz9920
    @tnnz9920 9 місяців тому

    sir, could you put results instead of only code on videos? anyway I like ur videos

  • @AhmadRosyidin-z3n
    @AhmadRosyidin-z3n Рік тому

    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 🙂

  • @bongbuincyril6150
    @bongbuincyril6150 11 місяців тому

    I love u bro

  • @muhammad_faraz_ali
    @muhammad_faraz_ali 8 місяців тому

    Great content, can you please share the source-code?

  • @nanonkay5669
    @nanonkay5669 9 місяців тому

    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.

  • @2gbeh
    @2gbeh Рік тому +1

    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  Рік тому

      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 Рік тому

      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 Рік тому

    make a video on zod.

  • @partiid
    @partiid Рік тому +1

    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  Рік тому +1

      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

  • @saadnouinou-j4j
    @saadnouinou-j4j Рік тому

    👍

  • @DazzyDude00
    @DazzyDude00 Рік тому

    You'd think experienced devs would use snippets. Not typing out: export default function... everytime 😂

    • @cosdensolutions
      @cosdensolutions  Рік тому +1

      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 Рік тому

    Avoid impure functions as we can this is the SRP

  • @TiberiusGracchi
    @TiberiusGracchi 2 місяці тому

    Passing objects to components is bad for performance, better to pass primatives

  • @qureshiowais6951
    @qureshiowais6951 8 місяців тому

    instead of theory we need how we can do !!! it

  • @rimha6846
    @rimha6846 Рік тому

    The SRP does not mean doing one thing

  • @hesoncontroler
    @hesoncontroler Рік тому

    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  Рік тому +1

      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

  • @MathForAll81
    @MathForAll81 Рік тому

    Hi, we need a playlist for different design pattern implementations in react 🙏🏻

  • @anonymous_basics
    @anonymous_basics Рік тому

    how i can test this components. and is it necessary to make tests for all the components ?

    • @cosdensolutions
      @cosdensolutions  Рік тому

      I honestly mostly only do e2e tests of the whole app, and not individual components

  • @nawazishali274
    @nawazishali274 Рік тому

    Can I connect with you on social media accounts to see timeline about your latest videos ,twitter ,insta or any other etc...

    • @cosdensolutions
      @cosdensolutions  Рік тому +1

      I'm not on twitter, but you can check me out here, on Instagram, or TikTok!

    • @nawazishali274
      @nawazishali274 Рік тому

      @@cosdensolutions Got it !

  • @anonymous_basics
    @anonymous_basics Рік тому

    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 😂