Create Highly Reusable React Components in Minutes with TypeScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ค. 2024
  • Let's look into a best practice for creating highly reusable React components. We'll use TypeScript for that, but JavaScript works just fine too. You'll learn a really helpful trick to get type completion on your component, saving you lots of maintenance and headache in the long run. Also, we'll conditionally display tailwind classes with a convenient utility function! :)
    ⭐ Our startup: wordful.ai
    ⭐ My GitHub: github.com/joschan21
    0:00 - Intro
    0:18 - The Problem
    3:27 - The Solution
    10:26 - Outro
    The stuff I use to make my videos
    Camera: amzn.to/3XcqRKO
    Light: amzn.to/3Xc1yIE
    Keyboard: amzn.to/3CKxnAi
    Mouse: amzn.to/3CNcfcm
    Microphone: amzn.to/3iybVHC
    Headphones: amzn.to/3IHTTgH
    thats pretty much it. Those are affiliate links so I might earn a commission if you purchase after clicking them. :^)
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @scumbag_kiko
    @scumbag_kiko ปีที่แล้ว +6

    Ive had this on my mind this past week, this video came just in time! Great job!

  • @snatvb
    @snatvb ปีที่แล้ว +19

    Exists better way: use React.ComponentProps and you will avoid all boilerplate, also you need to wrap component with forwardref - then it will really common component
    at least avoid interfaces for declare type of props, because this is final type, you don't imply that your props type may be extended from outside with merging declaration (actually it may be unintentional)

    • @MisouSup
      @MisouSup ปีที่แล้ว +4

      I do it like this all the time as well. I was wondering if this is common because I don't see people mentioning it so much.

    • @snatvb
      @snatvb ปีที่แล้ว +2

      ​@@MisouSup unforchantly not really :((
      Need to teach every developer follow this rules

    • @CoIdestMoments
      @CoIdestMoments ปีที่แล้ว

      @@snatvb How did you learn that to do it that way ?

    • @snatvb
      @snatvb ปีที่แล้ว +2

      @@CoIdestMoments somehow came with time

    • @gatocode316
      @gatocode316 11 หลายเดือนก่อน +1

      thanks, i was thinking that redeclaring the disabled property was not the best way

  • @xylarwardhan8445
    @xylarwardhan8445 ปีที่แล้ว

    Ok woah this is extremely useful! Thank you so much!

  • @51Grimz
    @51Grimz ปีที่แล้ว

    I've been playing around with this concept of spreading all props to components created from a generic component that calls reacts create Element function based on the element type then just spreads the props from a json object. Pretty cool low code concept.

  • @PedroFernandez-iv2uv
    @PedroFernandez-iv2uv ปีที่แล้ว

    great video Josh, really appreciate it

  • @francisagada5810
    @francisagada5810 ปีที่แล้ว

    this is great, Champ. I love this

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

    what if my custom component has input but also a label? how to pass attributes of 2 elements ?

  • @ya4dang1
    @ya4dang1 ปีที่แล้ว

    Do clsx and twMerge mess with Tailwind tree-shaking?

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

    You are a life savor. Thank you

  • @oleksandrluchytskyi6867
    @oleksandrluchytskyi6867 ปีที่แล้ว

    is it alias in you import … ‘@/components/…’ how to do this for create-react-app starter?

  • @mdkawsarislamyeasin4040
    @mdkawsarislamyeasin4040 ปีที่แล้ว

    Thanks, I didn't know that we can extend all other attributes from TS in our interfaces

  • @bunyameennurudeen173
    @bunyameennurudeen173 ปีที่แล้ว +3

    Great tutorial. Thanks for the lovely explanation

  • @nazarshvets7501
    @nazarshvets7501 ปีที่แล้ว +6

    ```
    type ButtonProps = React.HTMLButtonAttributes & {
    // ...custom props
    }
    ```
    `HTMLButtonAttributes` extends generic `HTMLAttributes` and add properties which only exist for buttons (like `type: 'button' | 'submit' | 'reset'`, `form: string`, `disabled: boolean` and so on)

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

    thank you! very clear

  • @RoshanKumar-sh8ve
    @RoshanKumar-sh8ve ปีที่แล้ว +5

    I personally like cva (class variance authority) for reusable components.. have u tried it?

    • @mohaniya15
      @mohaniya15 ปีที่แล้ว

      The latest video on my channel is about this!

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

    thank you but onClick is not working ? Can anyone help and say why its not

  • @codewithguillaume
    @codewithguillaume ปีที่แล้ว +1

    Really basic but really useful

  • @dev-suresh
    @dev-suresh 3 หลายเดือนก่อน

    Hi Josh, informative video, however, I came here to find the solution to how we can use event handlers as props as I am getting error that Event Handlers cannot be passed as props in nextjs

  • @ILoveTheMusic-rk7nr
    @ILoveTheMusic-rk7nr 5 หลายเดือนก่อน

    It's crazy thank you so much

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd หลายเดือนก่อน

    Thank you so much

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

    Now I know what old time devs mean when they say new devs are "reinventing" stuff, in this case OOP and inheritance.
    Not knocking btw, I think this is great!

  • @kulbirsingh8915
    @kulbirsingh8915 ปีที่แล้ว

    This is a great video, keep up the good work!! Can you explain how the ` 'cursor-not-allowed': isLoading ` works? How is that conditionally adding the class? shouldn't you use something like isLoading && ' cursor-not-allowed'' instead?

    • @joshtriedcoding
      @joshtriedcoding  ปีที่แล้ว +1

      That works fine too, however, when you inspect that in the browser and the condition is false you might have "false" or "undefined" as the class (not a huge deal tho). With this library, the syntax is slighty different as your pointed out, making those conditionals way easier to write and read

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

    Thanks josh

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

    Thank you

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

    Didn't you permanently diaabled button? Isn't it right to make {disabled} instead of your disabled?

  • @dimasadnan4873
    @dimasadnan4873 ปีที่แล้ว +2

    noob question : why use external class value package instead of using template literals?
    Edit : and use ternary for conditional classes

    • @okage_
      @okage_ ปีที่แล้ว +1

      it makes it easier to make conditional classes

    • @nicholascostadev
      @nicholascostadev ปีที่แล้ว +1

      tailwindMerge solves some mistakes that can happen at runtime like two conflicting classes, and classNames or clsx can make your code more readable if at large scale, meaning there are lots of conditional classes. You can think of a button not using any of these packages having 6 conditionals, would have lots of ternaries meaning that for some people it may be less readable. I don't quite use both packages, but I understand its use case.

    • @dimasadnan4873
      @dimasadnan4873 ปีที่แล้ว

      @@nicholascostadev still don't fully get it but might be because my lack of experience, I'll keep them in mind though. Thanks for the answers!

  • @gpyk306
    @gpyk306 10 หลายเดือนก่อน +1

    so he is extending it with "disabled" which is already HTMLButtonElement property, makes sense :D

  • @marcintichoniuk6554
    @marcintichoniuk6554 ปีที่แล้ว +3

    type Props = React.ComponentPropsWithoutRef & {
    customProp?: string;
    };
    You will have disabled attribute

  • @pheisar
    @pheisar ปีที่แล้ว

    Why is twMerge needed?

    • @jpvlog8129
      @jpvlog8129 10 หลายเดือนก่อน +1

      twMerge is helpful in merging or concatenating styles. Sometimes, the CSS you inputted may not effect and to solved that issue, you will need to add exclamation point (means important). It's kind of ugly.
      This issue can be solved by twMerge, no more prefix exclamation point and the CSS will work as expected.

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

    Why don't you use forwardRef?

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

    Gold

  • @namesare4fools
    @namesare4fools ปีที่แล้ว

    in the end you'll go full circle of using component libraries

  • @hardwired89
    @hardwired89 ปีที่แล้ว

    No library plss

  • @andrewc8125
    @andrewc8125 ปีที่แล้ว +4

    Hm kinda defeat the purpose of typescript if every attribute is optional

    • @imadmachi3227
      @imadmachi3227 ปีที่แล้ว

      That doesn't introduce any vulnerability, since the suggested properties are all supported by the corresponding html tags

    • @rusliabdulgani9920
      @rusliabdulgani9920 3 หลายเดือนก่อน +1

      nope, optional is not mean you pass the type strict, cause the value is just its original value or undefined, you will ignore the power of typescript if you pass it using "any" type..

  • @mohaniya15
    @mohaniya15 ปีที่แล้ว

    dude i cant believe it I just released a similar video on this yesterday

    • @zoki5388
      @zoki5388 ปีที่แล้ว

      frontendmasters was inspiration ?

    • @mohaniya15
      @mohaniya15 ปีที่แล้ว

      @@zoki5388 no i am not aware of who that is, but i will check them out!