A Better Way To Handle Vue Props?

Sdílet
Vložit
  • čas přidán 21. 02. 2024
  • 🤖 WebDevDaily: www.webdevdaily.io
    📕 Nuxt 3 Course: www.learnnuxt.dev/
    📕 Vue 3 Crash Course: johnkomarnicki.gumroad.com/l/...
    🚀 Join the channel : / @johnkomarnicki
    🤖 Website: johnkomarnicki.com/
    🐦 Twitter: / john_komarnicki
    #vue #vue3 #nuxt #nuxt3
  • Věda a technologie

Komentáře • 37

  • @julisch94
    @julisch94 Před 4 měsíci +2

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

  • @brendonvz
    @brendonvz Před 5 měsíci

    Great simple video!

  • @karlstenator
    @karlstenator Před 5 měsíci +1

    Great video, thanks for sharing.

  • @cindysi63
    @cindysi63 Před 5 měsíci

    Nice tip. Thanks

  • @russianstan
    @russianstan Před 5 měsíci +1

    What about inheriting attributes in child component with this approach?

  • @alimaher1
    @alimaher1 Před 5 měsíci +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  Před 5 měsíci +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

  • @aqordeon
    @aqordeon Před měsícem

    this video develop my Vue skill. thanks.

  • @hinogi
    @hinogi Před 4 měsíci

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

  • @joshuaberrios
    @joshuaberrios Před 5 měsíci

    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  Před 5 měsíci

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

  • @pavelino17
    @pavelino17 Před 5 měsíci

    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  Před 5 měsíci +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 Před 5 měsíci

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

  • @nguyennguyen6924
    @nguyennguyen6924 Před 5 měsíci

    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  Před 5 měsíci

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

  • @Stefano-eo
    @Stefano-eo Před 5 měsíci

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

  • @RomanDawydkin
    @RomanDawydkin Před 5 měsíci +2

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

    • @JohnKomarnicki
      @JohnKomarnicki  Před 5 měsíci +1

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

  • @yoanestradablanco1608
    @yoanestradablanco1608 Před 5 měsíci

    Good video

  • @Saeid-Za
    @Saeid-Za Před 5 měsíci +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  Před 5 měsíci +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!

  • @cant_sleeeep
    @cant_sleeeep Před 4 měsíci

    dude, just go with the defineprops macro

  • @MightyArts
    @MightyArts Před 5 měsíci

    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  Před 5 měsíci +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!

  • @ForexLearnerAI
    @ForexLearnerAI Před 5 měsíci +8

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

    • @JohnKomarnicki
      @JohnKomarnicki  Před 5 měsíci +3

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

    • @srdnlmrn
      @srdnlmrn Před 5 měsíci

      but the title says the better way 😂😂😂

    • @JohnKomarnicki
      @JohnKomarnicki  Před 5 měsíci +2

      It’s a question title

  • @Kingside88
    @Kingside88 Před 4 měsíci

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

  • @user-of6ls2ng5l
    @user-of6ls2ng5l Před 25 dny

    Questionable approach, brother.

    • @JohnKomarnicki
      @JohnKomarnicki  Před 25 dny

      What’s questionable?

    • @user-of6ls2ng5l
      @user-of6ls2ng5l Před 25 dny

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