No video

A Trick Every React Developer Should Know: Ref Forwarding

  Рет қаралды 19,705

Josh tried coding

Josh tried coding

Күн бұрын

Пікірлер: 36
@dennisadamczyk5067
@dennisadamczyk5067 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Bro you saved me. I literally was mindblowed I dont know how I never came across this while learning react
@joshtriedcoding
@joshtriedcoding Жыл бұрын
Cheers man!
@welserrano
@welserrano Жыл бұрын
Exactly what I needed. Thanks and keep doing great content like this!
@kinzeyrahardja2600
@kinzeyrahardja2600 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Could you make a video on passing refs between child & parent components along with *useImperativeHandle* hook ?
@kc-bytes743
@kc-bytes743 Жыл бұрын
new thing to learn , You genius you make difficult concept easy
@taiwodamilola8636
@taiwodamilola8636 Жыл бұрын
Properly explained 👍🏼, thanks man do a full react typescript.tutorial..
@anmoljhamb9024
@anmoljhamb9024 Жыл бұрын
Thank you for the tutorial man! I was looking for this exact thing!
@joshtriedcoding
@joshtriedcoding Жыл бұрын
verrry nice cheers
@shawn-skull
@shawn-skull 11 ай бұрын
Why didn't you just rename the *ref* so it be treated as a prop instead of an attribute. It works for me
@AngelEduardoLopezZambrano
@AngelEduardoLopezZambrano Жыл бұрын
You could have named the ref prop something other that ref. It would have worked as well.
@ivangemota1527
@ivangemota1527 Жыл бұрын
what name of extension did you use for typescript snippets?
@adamrodriguez7598
@adamrodriguez7598 Жыл бұрын
if you start typing log instead of console.log then it will auto complete @7:25
@prasathj7436
@prasathj7436 11 ай бұрын
Excellent, thanks a lot !!!
@timekouka
@timekouka Жыл бұрын
great one brother!
@iwantfrens5804
@iwantfrens5804 Жыл бұрын
Hey Josh. Why do you use Firefox?
@ksas323
@ksas323 7 ай бұрын
Thank you
@filipesommer8253
@filipesommer8253 Жыл бұрын
What is the extension to see the gzip size on imports?
@illmatic08
@illmatic08 Жыл бұрын
import cost
@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
@wakinki
@wakinki 6 ай бұрын
Why stop using typescript in the middle of the video ?
@idobleicher
@idobleicher Жыл бұрын
nice video.
@joshtriedcoding
@joshtriedcoding Жыл бұрын
thanks man!
@soymartiinez
@soymartiinez Жыл бұрын
Inspired on shadcn.
@joshtriedcoding
@joshtriedcoding Жыл бұрын
His work is great
@amotekundev
@amotekundev 9 ай бұрын
@@joshtriedcoding The way you aknowledge others is really commendable
@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 9 ай бұрын
🤣
@0xPanda1
@0xPanda1 Жыл бұрын
🥰
Goodbye, forwardRef
15:59
UI Engineering
Рет қаралды 7 М.
Mastering React Context 2023: The Complete Guide!
24:37
Josh tried coding
Рет қаралды 14 М.
managed to catch #tiktok
00:16
Анастасия Тарасова
Рет қаралды 47 МЛН
拉了好大一坨#斗罗大陆#唐三小舞#小丑
00:11
超凡蜘蛛
Рет қаралды 13 МЛН
Matching Picture Challenge with Alfredo Larin's family! 👍
00:37
BigSchool
Рет қаралды 45 МЛН
Violet Beauregarde Doll🫐
00:58
PIRANKA
Рет қаралды 25 МЛН
3 React Mistakes, 1 App Killer
14:00
Jack Herrington
Рет қаралды 115 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 472 М.
How Did I Not Know This TypeScript Trick Earlier??!
9:11
Josh tried coding
Рет қаралды 210 М.
React Hook useRef and forwarding refs with forwardRef
14:46
techsith
Рет қаралды 81 М.
Never Forget React forwardRef Again
9:33
Coding in Public
Рет қаралды 17 М.
How React Query Won
34:52
Theo - t3․gg
Рет қаралды 74 М.
I DONT USE NEXT JS
54:01
ThePrimeTime
Рет қаралды 349 М.
React 19 has a Problem
7:43
Josh tried coding
Рет қаралды 31 М.
You might not need useEffect() ...
21:45
Academind
Рет қаралды 158 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 169 М.
managed to catch #tiktok
00:16
Анастасия Тарасова
Рет қаралды 47 МЛН