Fix your useRefs with ElementRef

Поділитися
Вставка
  • Опубліковано 24 лип 2023
  • Article: www.totaltypescript.com/stron...
    Become a TypeScript Wizard with my free beginners TypeScript Course:
    www.totaltypescript.com/tutor...
    Follow Matt on Twitter
    / mattpocockuk
    Join the Discord:
    mattpocock.com/discord
  • Наука та технологія

КОМЕНТАРІ • 38

  • @rand0mtv660
    @rand0mtv660 11 місяців тому +57

    I wasn't sold on the first example because I never found that one problematic, but second example with a custom component is definitely super helpful.

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

    Thanks. Didn't knew about this helper. That's very helpful.

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

    Great, thanks Matt!

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

    Fantastic, that's right. Thanks Matt! You're the best

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

      Darn how did I not know this until now

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

    Thank you soo much for this one!
    Whish I knew this way earlier.

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

    great job!

  • @Kimi-xp2th
    @Kimi-xp2th 10 місяців тому

    wow thanks a lot. This was exactly what I was looking for. Subbed and liked

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

    Oh wow. I had no idea about this! A true gem

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

    Thanks. I used to grab the type from the ref property

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

    Really cool!

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

    Thanks!

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

    I love your videos especially when ts is combined with react in a proper way. Do you have plans on speaking about the combo react material ui ? That'd amazing

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

    wish i knew this last week. i ended up creating my own helper functions for the forwardRef

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

    Nice tip

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

    TIL. thanks man!

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

    tks bro!

  • @ApprendreSansNecessite
    @ApprendreSansNecessite 11 місяців тому +5

    The second use case has clear added value. Why would a component know about what is merely passing through it? I'm not a fan of the first one though, because we are replacing a native thing that, as a reader, you are supposed to know or recognise right away with some arbitrary React thing that you will have to look up and that is taking a string that is no more readable than the native type. Moreover, the API is confusing because it takes either a string or a component type. This would (and I disagree) be improving the experience of the writer at the expense of the reader, which I think is backwards as most devs read more code than they write.

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

    What should I use forward ref or componentPropswithref and what is the diff between these things?

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

    The IDE-like code snippets (with the hover hints) on your site are so cool! Do you do it on the client side?

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

      It's using shiki-twoslash, then some server-side magic to get it working with our CMS.

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

    How is ElementRef different from ComponentRef? Or are they the same but from different React versions?

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

    Matt, this type uses the HTMLElementTagnameMap type under the hood, right?

  • @user-kx4nc4um8v
    @user-kx4nc4um8v 6 місяців тому

    And what about ref to component where useImperativeHandle is used to add some property?
    the "original" ref is done internally there, and outside is the ref to useImperativeHandle.
    How to manage this ref type?

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

    Didn't get how to make for ex. a hook which is returning ref which could be assigned to different type of elements (react components different html jsx in return). Seems to be generic with passed in type = typeof 'target component', but ts not happy when I try to reach for ex. ref.current.contains property

  • @monfernape
    @monfernape 11 місяців тому +2

    Not super clear how `ElementRef` is inferring from `OtherComponent` that we're referencing a table?

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

      That component expects a table ref.
      You wouldn't know that unless you looked at the component's internal definitions - OR used ElementRef.

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

      @@mattpocockuk ah I see. That makes complete sense

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

    Hello,
    Nice and Helpful content.
    Can you please make video on how to use typescript with API calls, where we don't know which type of data we will be receiving.
    It would be very helpful to know how to tackle this situation.
    Thanks

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

      you can use zod for schema validation if you want to be sure you're only processing data that you expect.

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

      @@FunctionGermany Thanks, will check that out

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

    I’m shocked you don’t use type only imports in your samples?

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

    So this is like componentProps but for refs?

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

    Does this not work? ElementRef

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

      Yep, works great, but isn't very easily discoverable.

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

    Good luck with its big brother: "forwardRef"

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

    HeytshTML