A Better Way To Handle Vue Props?

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ส.ค. 2024
  • 🤖 WebDevDaily: www.webdevdail...
    📕 Nuxt 3 Course: www.learnnuxt....
    📕 Vue 3 Crash Course: johnkomarnicki...
    🚀 Join the channel : / @johnkomarnicki
    🤖 Website: johnkomarnicki...
    🐦 Twitter: / john_komarnicki
    #vue #vue3 #nuxt #nuxt3

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

  • @alimaher1
    @alimaher1 6 หลายเดือนก่อน +4

    Nice video, but I'm not sure if this idea scales well with bigger objects with many props and some nested objects.

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +3

      Hey, thanks for the comment.
      First, i don't see an issue with nested objects. It would work the same, it would just be an object within the component.
      Secondly, if your component has many props, even though it would require a bit more code to define all these props, to me it would still be a good idea. This way you know what exactly is being used within the component. Looking at a big single object as a prop to me is very granular.
      But it's all preference at the end of the day. There really is no wrong way

  • @julisch94
    @julisch94 5 หลายเดือนก่อน +2

    I personally don't like to use the ["field"] accessor because it feels error-prone. What if I spell field wrong?

    • @TotalImmort7l
      @TotalImmort7l 13 วันที่ผ่านมา

      typescript will show error: field not found

  • @russianstan
    @russianstan 6 หลายเดือนก่อน +1

    What about inheriting attributes in child component with this approach?

  • @karlstenator
    @karlstenator 6 หลายเดือนก่อน +1

    Great video, thanks for sharing.

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

      Thanks for watching!

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

    this video develop my Vue skill. thanks.

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

    So, what about if you have two objects? I guess you can't use multiple v-binds but how would you handle that scenario?

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

    Great simple video!

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

    Gained a subscriber out of me.
    This was great. Never thought of doing this. Going to try to implement this next week at my job.

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

      Awesome! Be sure to let me know how that goes!

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

    this is more likely a personal taste , there is no bad side passing an object via props so we dont have to define so many props inside child component , this takes less time . but I agree with you at readability point .

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

      Yeah, as I mentioned, the other ways I showed we’re not wrong. Just a preference at the end of the day

  • @Saeid-Za
    @Saeid-Za 6 หลายเดือนก่อน +1

    Why not use the whole User as a root prop for the child?
    Somthing like "defineProps()" and if I want to only use some fields of User (not the whole User) using built-in Pick or Omit would be helpful. In this scenario calling and passing the Component and the Component code would be as minimal as possible

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +2

      I think this would work? However, as i mentioned the other ways i showed were not wrong. This approach i showed is just another way of handling it. At the end of the day it's all up to preference and what you prefer!

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

    Nice tip. Thanks

  • @RomanDawydkin
    @RomanDawydkin 6 หลายเดือนก่อน +2

    An interesting but controversial approach. What if we need to add another property later?

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +1

      You would just add it as a prop in the component. Would be very simple to do

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

    Nice video, does this also work with passing additional props that are not in the User object? say any additional text. Can i then go v-bind="User" :text="myText" and just add the text prop to the defiineProps?

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +2

      Yeah, I believe that should work.
      You’d just have to ensure you don’t pass a prop name already being declared in that object otherwise you might encounter some issues.

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

      @@JohnKomarnicki cool thanks, I'll try it out

  • @Stefano-eo
    @Stefano-eo 6 หลายเดือนก่อน

    If you destruscture the props, are you not losing the ref?

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

    dude, just go with the defineprops macro

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

    I don't like this approach. Why would you do that?

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +3

      Readability.
      There’s no right or wrong way to do this. It’s mainly about preference, and what you prefer

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

      but the title says the better way 😂😂😂

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +2

      It’s a question title

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

    Good video

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

    Why don't you just define the User type within the UserCard component itself, instead of defining it somewhere else, and then importing it here? Collocate the types, it will achieve the same readability that you're looking for here.

    • @JohnKomarnicki
      @JohnKomarnicki  6 หลายเดือนก่อน +1

      Typically you'd have this object defined in the parent component, so it would need to be typed there. So creating an external type does make sense in this case. Yes, the types in the file would show what exactly the object is, but to me it's just not very clear. This approach i showed is just another way of handling it. At the end of the day it's all up to preference and what you prefer!

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

    Very opinionated. To be hones I liked the first approach the most. Just :user="user" .

  • @user-of6ls2ng5l
    @user-of6ls2ng5l หลายเดือนก่อน

    Questionable approach, brother.

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

      What’s questionable?

    • @user-of6ls2ng5l
      @user-of6ls2ng5l หลายเดือนก่อน

      @@JohnKomarnicki If I want to add/remove keys in User, I will have to specify them in the component