How I Write Clean Code in React

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 77

  • @fanespratama
    @fanespratama 8 หลายเดือนก่อน +3

    Take a note that when you use the custom hook with useEffect in more than one file, the useEffect will run more than once, so keep one custom hook for every layout/component (let say one for user list, one for user details). If not, just move the useEffect outside the custom hook

  • @johndevnoza4223
    @johndevnoza4223 8 หลายเดือนก่อน +6

    as a begginer one, im doing this by habbit and i was scared if it was overkill or not. now i feel happy!

  • @dr_jacko
    @dr_jacko 8 หลายเดือนก่อน +8

    This is a good start, but in general I would prefer to abstract fetch away a step further than hooks or components and you also need to handle loading and error states with your async calls.
    As an example, create a UserProfileRepository and UserProfileService classes which have functions for CRUD on UserProfiles. Use the singleton pattern on these classes and keep them in a context where it is required in the app.

    • @vladanisov1689
      @vladanisov1689 8 หลายเดือนก่อน

      definitely the best approach for this kind of scenario!

  • @sachityadav4141
    @sachityadav4141 8 หลายเดือนก่อน +1

    This was Needed 😀 ThankYou, i was having anxiety seeing lots of styling classes in return of every component

  • @calmmind3160
    @calmmind3160 5 หลายเดือนก่อน

    mate, i'm enjoying your react content, this is what i need to learn, keep making content

  • @shoaib_akhtar_1729
    @shoaib_akhtar_1729 6 หลายเดือนก่อน

    Thanks for explaining the importance of achieving modularity in our code.

  • @seandiamond7983
    @seandiamond7983 หลายเดือนก่อน

    Thank's for your content, my brother.

  • @mulengachitalima7695
    @mulengachitalima7695 8 หลายเดือนก่อน

    Awesome video.. Really appreciate how you always adhere to SOLID principles in your videos

  • @ianfrye8988
    @ianfrye8988 8 หลายเดือนก่อน +4

    Great video. Only question I have: why not move the useFetchProfile inside the ProfileData page instead of passing it as a prop?

    • @ExileEditing
      @ExileEditing 8 หลายเดือนก่อน +1

      I struggle with choosing between fetching on the page and passing down or fetching in the components as well. I think it makes sense for both, but it is better if you separate concerns and fetch from the page. This way, your component only cares about rendering and doesn't deal with how it gets the data. It makes those components simpler as well and probably easier to test

    • @cosdensolutions
      @cosdensolutions  8 หลายเดือนก่อน +6

      because ProfileData is only responsible for rendering, not fetching. Also, if you want to show profileData but with different filters or page sizes, adding that inside will be a mess. The way I did it, the parent decides that and just gives it to ProfileData to take over. It's simpler

    • @ianfrye8988
      @ianfrye8988 8 หลายเดือนก่อน

      @@cosdensolutions Thanks for clarifying!

    • @sontranvan5348
      @sontranvan5348 8 หลายเดือนก่อน

      ​@@cosdensolutions but in case I need to use the data of filters or page sizes from ProfileData to refresh profiledata, should I use a callback to left it to ProfilePage or move fetching into ProfileData component?

  • @meylis_so2
    @meylis_so2 6 หลายเดือนก่อน

    cosden how are u making sidebar in vscode and how u are controlling it please make tutorial

  • @mashab9129
    @mashab9129 8 หลายเดือนก่อน +1

    why not having ProfileData component call the useFetchProfile ?

  • @sontranvan5348
    @sontranvan5348 8 หลายเดือนก่อน

    Simple, effective tips that are easy to learn and apply. Thank you for sharing your knowledge!!!!

  • @Makłowiczwpodróży-q6l
    @Makłowiczwpodróży-q6l 8 หลายเดือนก่อน

    Perfect timing, just wanted to learn about those :D

  • @roshanmaddumage9675
    @roshanmaddumage9675 5 หลายเดือนก่อน

    I'm truly captivated by your content and watch it daily during my free time. Please continue to create great content. Regarding this video lesson, I have a question: How do you plan to organize the subcomponent or child component files when you break down a large page into smaller components?

    • @cosdensolutions
      @cosdensolutions  5 หลายเดือนก่อน +1

      Thank you for the kind words! You follow the single responsibility principle. Each component only does one thing until you've run out of things to do. So page component renders page and all features on page, then each feature gets its own component for the feature, and each one of those uses other components that make up the feature. There isn't really a strict rule as to how exactly, but rather it all depends on the features themselves and how logical it is to separate them. But generally following the SRP is a great way

    • @roshanmaddumage9675
      @roshanmaddumage9675 4 หลายเดือนก่อน

      @@cosdensolutions Great! I had a small doubt and now it's cleared. Thank you for the reply. ☺

  • @hooooman.
    @hooooman. 7 หลายเดือนก่อน +1

    A beginner here, this approach definitely makes our code more clean. but my doubt is, say if we are building a little advanced/complex project, the same approach makes too many folders and files ?

    • @carlostalavera4964
      @carlostalavera4964 23 วันที่ผ่านมา

      Look up screaming architecture. Keep your code modular and it doesn't matter how many folders and files you have (when it becomes insanely complex then you need microservices or microfrontends, but that's not the common case, you usually use a monolith, i. e., a single application doing everything, instead of lots of independent services with a single purpose). It's way better than having huge files doing lots of things. For example, if you're looking for changing how the profile data is displayed, would you be looking for ProfilePage or ProfileData? The second one seems more intuitive, because it's straightforward. That's the goal. Make everything as intuitive as possible, so navigating through the codebase is a breeze.

    • @hooooman.
      @hooooman. 23 วันที่ผ่านมา

      @carlostalavera4964 thankyou !!

  • @MohamedRabi
    @MohamedRabi 8 หลายเดือนก่อน

    Thank you for the fantastic content!

  • @viophile
    @viophile 8 หลายเดือนก่อน +2

    What is your view on default exports? In my opinion they should only be used in libraries, not in any other code.

    • @mahendrasinh_9106
      @mahendrasinh_9106 8 หลายเดือนก่อน

      if you export as a default then you can import it at any name
      // Exporting a component with export default
      const MyComponent = () => {
      // Component implementation
      };
      export default MyComponent;
      import MyComponent from './MyComponent';
      without export default
      // Exporting a component with named export
      export const MyComponent = () => {
      // Component implementation
      };
      // Importing the component
      import { MyComponent } from './MyComponent';
      you need to call with same name

    • @PaulSebastianM
      @PaulSebastianM 8 หลายเดือนก่อน +2

      Default exports should be banned. They are not adhesive to strong typing and make tree shaking impossible especially when mixed with named export.

  • @SahilPatel-kh2rt
    @SahilPatel-kh2rt 8 หลายเดือนก่อน

    Hey, which vs code theme you are using.? Excited to use it for my self also.

  • @edward-vonschondorf-dev
    @edward-vonschondorf-dev 8 หลายเดือนก่อน +1

    Great video Cosden! It's definitely something I've been looking into lately cause I don't like long complex components that I have to edit/create. I was just curious about something though, i noticed in your imports you have an @ symbol at the beginning and was wondering how that was setup and if there is an advantage to it? Thanks!

    • @cosdensolutions
      @cosdensolutions  8 หลายเดือนก่อน +1

      it's import aliases, the advantage is it makes things cleaner, and if you move things you don't have to always have everything be rewritten

  • @moonshadowwlb
    @moonshadowwlb หลายเดือนก่อน

    Thanks, ill try this in old prijects to see if i can improvement

  • @2positive1
    @2positive1 7 หลายเดือนก่อน

    I would move static data array upper, from the App component. It’s static, no need to recreate it on every re-render.

  • @64revolt
    @64revolt 8 หลายเดือนก่อน

    What do you think of atomic design and styled components? Only got to the page layout so far, but that seems like a perfect case for that, no?

  • @kalideb-y3y
    @kalideb-y3y 8 หลายเดือนก่อน

    should I still separate the fetching hook if only one component uses it?

  • @carloseduardomagalhaespere5112
    @carloseduardomagalhaespere5112 8 หลายเดือนก่อน

    Thank you for another great video! I've been learning a lot here!
    One question: Why do you prefer to use type instead of interface in the props definition? 🤔

  • @skymasseffect
    @skymasseffect 4 หลายเดือนก่อน

    can you make a video on MVVM architecture with react ?

  • @kit3x663
    @kit3x663 8 หลายเดือนก่อน

    I have one question. Why you didn't use React.FC or forwardRef in any of your videos. Is it usseles to use?

  • @JericAdriano-yo9pv
    @JericAdriano-yo9pv 8 หลายเดือนก่อน

    How about modals or dialogs that are used in one page? Do we handle them all in the parent component?

  • @tunde6159
    @tunde6159 8 หลายเดือนก่อน

    Such good tutorials.

  • @QueeeeenZ
    @QueeeeenZ 8 หลายเดือนก่อน

    I think you can use Nuxt and all the layout, structure will be already configured for you out of the box.

  • @delightful209
    @delightful209 8 หลายเดือนก่อน +1

    is it me or is the audio levels on this video really low. i have my headphones at 50 % and its quite low compared to other videos

  • @techaddictdude
    @techaddictdude 8 หลายเดือนก่อน

    Shouldn't the PageLayout.tsx be located in the pages folder, while the 'Profile' file should be in the in components?

  • @sathirabandara7981
    @sathirabandara7981 8 หลายเดือนก่อน

    great video. its very useful. 🤩

  • @lyrical6852
    @lyrical6852 8 หลายเดือนก่อน

    Honest question, are you doing this in the real world app or in your work?

  • @devquen37
    @devquen37 7 หลายเดือนก่อน

    PageLayout -> ProfilePage -> ProfileData -> Children Components (Avatar, PostList, etc)

  • @harithsenevi4
    @harithsenevi4 8 หลายเดือนก่อน

    Can't we just use the useFetchProfile hook inside the ProfileData component itself instead of passing profileData as props from ProfilePage component? Is it a bad practice? What complications could have with that approach?

  • @FiguringOutWhatIsLife
    @FiguringOutWhatIsLife 8 หลายเดือนก่อน

    I mean isn't the children example you gave in the first one just what we do in App.jsx though?

  • @bruce_k3lly
    @bruce_k3lly 8 หลายเดือนก่อน

    Thank you for this amazing video it's really helpful and it has increased my productivity, but can I use utils folder, as you said and just add reusable function for similar work ?

  • @programmerjowo
    @programmerjowo 8 หลายเดือนก่อน

    How to handle authentication on the API

  • @isaacjames8735
    @isaacjames8735 8 หลายเดือนก่อน

    Make a video about Social Login system in react please

  • @FalioV
    @FalioV 8 หลายเดือนก่อน +1

    Why would you use this Layout component on every page like this ? Just pass it to the index route and thats it. Overall I see this is "basic 101" and way far from Sr. react code. And I personally think this is not the best way to describe "custom hooks" as it should be. This profile code belongs to the Service layer. Don't get me wrong, I'm not here to argue, just have some questions over all. :)

  • @keklamo1485
    @keklamo1485 8 หลายเดือนก่อน

    anyone know what allows him to do rjsfc and have the react component with function keyword? i just have rafc and its the arrow funciton i hate it

    • @cosdensolutions
      @cosdensolutions  8 หลายเดือนก่อน

      check out my vscode setup video

    • @nicus1504
      @nicus1504 8 หลายเดือนก่อน

      why would you use export default function instead of export const?

    • @keklamo1485
      @keklamo1485 8 หลายเดือนก่อน

      @@nicus1504 i just like using function better its personal preference

  • @RoomCorner_
    @RoomCorner_ 8 หลายเดือนก่อน

    Sir i'm interest of React Project but my english isn't very well. Is there subtitle like in TH-cam?

  • @SingleSeeker
    @SingleSeeker 8 หลายเดือนก่อน +1

    in a real project , a loading , error is also needed.

  • @Divyv520
    @Divyv520 8 หลายเดือนก่อน

    Hey Man , really nice video ! I was wondering if I could help you with Viral Content ideas in your niche , Script , editing , thumbnail , SEO , Growth and analysis , Leads and appointments for your business , etc in all to you ! Pls let me know what do you think ?

  • @shakapaker
    @shakapaker 8 หลายเดือนก่อน

    props with children makes children optional, for layout it needs to be required

  • @Edgars82
    @Edgars82 8 หลายเดือนก่อน

    export const router = createBrowserRouter([
    {
    path: '/',
    element: (



    ),
    errorElement: ,
    children: [
    {
    index: true,
    element: ,
    },
    {
    path: 'search',
    element: (...
    this way all childrens will have layout and no need to import it as wrapper inside page components

  • @PeterStJ
    @PeterStJ หลายเดือนก่อน

    I would not agree with the final advice in the video (namely "build a habit of splitting...you will never build messy components") - the pattern you are prescribing for everyone and everything is called 'separation of concerns' and to that I usually say to less experienced devs: start separating when you get concerned. In your case - start extracting functionality when you see that it is reusable (like the layout is in your video, but not the profile fetching - your approach for fetching also has other issues - some commenter mentioned it bellow) but not before that. Otherwise great video (and most of the other videos you made).

  • @CyberSurgeon
    @CyberSurgeon 8 หลายเดือนก่อน

    in your video description ,the project react lik is give me 500 web code .

  • @-mahmoudadel2628
    @-mahmoudadel2628 8 หลายเดือนก่อน +1

    يمزاجك يا كوزدين

  • @NikulPipariya-hg5mb
    @NikulPipariya-hg5mb 8 หลายเดือนก่อน +11

    Increase your voice please... Its so low.... Thanks for zooming screen

    • @tjdgmlchl6305
      @tjdgmlchl6305 8 หลายเดือนก่อน +10

      nah I like it because he’s not yelling or doing a “youtuber voice”

    • @adeemmmm
      @adeemmmm 8 หลายเดือนก่อน +1

      His volume level is good,try to check your speaker

    • @dr_jacko
      @dr_jacko 8 หลายเดือนก่อน +2

      If only there was a way to increase the volume of a video??!?

    • @Pilosofia
      @Pilosofia 8 หลายเดือนก่อน +1

      throw your speakers in the trash and get new ones.

    • @2positive1
      @2positive1 7 หลายเดือนก่อน

      Volume is good.