A Trick Every React Developer Should Know: Ref Forwarding

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

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

  • @dennisadamczyk5067
    @dennisadamczyk5067 ปีที่แล้ว +25

    Actually, using useEffect with a ref in the dependency array does not work as expected because refs are not part of reacts component dependency system. A component does not rerender when you change the current value of a ref, so the useEffect will also not run in this case.

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

      Thanks for the comment. Ive learned from you.
      import { useRef, useEffect } from 'react';
      function MyComponent() {
      const myRef = useRef(null);
      useEffect(() => {
      console.log('useEffect ran');
      console.log(myRef.current);
      }, [myRef]);
      const handleClick = () => {
      myRef.current = 'new value';
      };
      return (

      Change ref value

      );
      }
      In the example above, we have a useEffect hook with a dependency array that includes myRef. When the component mounts, the useEffect callback runs and logs 'useEffect ran' and the current value of myRef to the console.
      However, when we click the button to change the current value of myRef, the useEffect does not run again. This is because refs are not part of the React component dependency system, so changing the value of a ref does not trigger a re-render or cause the useEffect hook to run again.

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

      I had cases where this was exactly the behaviour I needed :P

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

      @@martapfahl940 In this case you could also leave the dependency array empty. There is no point in adding the ref as dependency for the useEffect then.

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

      @@dennisadamczyk5067 You're actually right I mixed up 2 different topics...

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

    Bro you saved me. I literally was mindblowed I dont know how I never came across this while learning react

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

    Exactly what I needed. Thanks and keep doing great content like this!

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

    Does putting [ref] as the dependency array for useEffect work? I thought useRef doesn't create any component re-renders like useState.
    Or perhaps is it that whenever the "ref.current" changes, no re-render is done, but changing the value of "ref" causes a re-render

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

    Nice video, Could also have added the usage of useImperativeHandle hook, this would have given the viewers more insight to use the forwarded refs

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

    Could you make a video on passing refs between child & parent components along with *useImperativeHandle* hook ?

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

    Properly explained 👍🏼, thanks man do a full react typescript.tutorial..

  • @kc-bytes743
    @kc-bytes743 ปีที่แล้ว

    new thing to learn , You genius you make difficult concept easy

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

    Thank you for the tutorial man! I was looking for this exact thing!

  • @shawn-skull
    @shawn-skull ปีที่แล้ว +2

    Why didn't you just rename the *ref* so it be treated as a prop instead of an attribute. It works for me

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

    if you start typing log instead of console.log then it will auto complete @7:25

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

    You could have named the ref prop something other that ref. It would have worked as well.

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

    Hey Josh. Why do you use Firefox?

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

    what name of extension did you use for typescript snippets?

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

    🎉 how to accept generic with forward ref().
    i am using forward ref with react hook form, so i need to pass the control and need to make the types generic so that based on the control i get name="" auto suggestions

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

    Excellent, thanks a lot !!!

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

    Thank you

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

    great one brother!

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

    What is the extension to see the gzip size on imports?

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

    nice video.

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

    Inspired on shadcn.

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

      His work is great

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

      @@joshtriedcoding The way you aknowledge others is really commendable

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

    Why stop using typescript in the middle of the video ?

  • @0xPanda1
    @0xPanda1 ปีที่แล้ว

    🥰

  • @boris---
    @boris--- ปีที่แล้ว

    Back to flipping burgers... I have no idea what just happened in this video.. I watching this my 3th time.. and still don't understand even where you possibly want use ref...

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

      🤣