React TypeScript Tutorial – 16 – useRef Hook
Video Title: React TypeScript Tutorial – 16 – useRef Hook
Welcome back everyone in this video let’s take a look at the final hook which is the use ref hook when it comes to usref there are primarily two scenarios as a read-only ref for a dom element or as a mutable value which can behave like an instance variable
Let’s look at typing both those scenarios in the components folder i’ve created another folder called ref within the folder i’ve created two files one for each of the scenarios i’ve just mentioned let’s begin with domref.tsx this file contains a component where we use the ref hook to focus an input
Element when the component mounts let me go over the code we first invoke the user f hook passing in null as the initial value the return value is stored in a constant called input ref we set this constant equal to the ref prop on the input element we then access
The dom element in the use effect hook to focus the input pretty straightforward use case which you might have seen when learning about the user f hook of course we have the typescript errors which we are now going to solve if you hover over the error you can see that typescript is complaining
Object is possibly null so we can’t call focus on null to fix this we can simply make a check using optional chaining so current question mark dot focus when we do that we get a new error property focus does not exist on type never now this has to do with the reference type
And to fix this we have to specify the type of the dom element since input is the html element we are referencing after user f on line four add angle brackets and specify html input element and when we do that typescript is happy now there are different types of html
Elements so pick the one that suits your requirement a bonus tip here is if you’re sure your reference is never null when you access it you can add the non-null assertion when invoking user f so after null add the exclamation sign this allows us to call focus without optional chaining
So these are some of the points to keep in mind when using the use ref hook for dom references let’s now take a look at the second scenario to manage mutable refs with the user f hook now i have another file called mutableref.tsx the component does seem complex but it
Really is not it is a simple timer component let’s go over the code first we have a timer state variable to keep track of the number of seconds we then create a reference and store it in a constant called interval ref when the component mounts we invoke the set interval function
To increase the timer value by 1 every second the written value of set interval is stored in interval ref dot current so we are mutating the reference value to make sure the timer is cleared when the component unmounts we call the stop timer function inside stop timer we call clear interval passing in
Interval ref dot current which holds the interval id i’ve also added a button to stop the timer let’s now fix the typescript errors first let’s hover over the error inside use effect type number is not assignable to type null now if you’re not aware set interval
Returns a numeric value which is the id of that interval however we have told typescript that usref is of type null only so let’s add number or null as the type angle brackets number or null with that our first error is fixed if i hover on the error inside stop timer
Argument of type number or null is not assignable to parameter of type number or undefined now you could change null to undefined and the error is fixed or you can leave it at null and make a check inside stop timer as well so if interval ref dot current
Only then call window dot clear interval passing in the appropriate value typescript is happy once again so this is pretty much how you type the userfork for dom references specify the dom element type for mutable references specify the appropriate type for example number in case of set timeout or set interval
With that we can conclude typing all the hooks in react thank you all for watching i’ll see you in the next video
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Use coupon code CodevolutionYoutube for 100% off lifetime discount to your subscription at https://www.taskade.com/billing
Thanks for your videos… can you share the repository to review your videos. they are excellent.
i have your videos on replay. so far i got 25 projects in react, and this is my second time writing types for all of them. i love these videos. thank you <3
If anyone face the below kind of error for inputRef.current inside useEffect
typescript error Type 'Timer' is not assignable to type 'number'.ts(2322)
(property) React.MutableRefObject<number | null>.current: number | null
please use the type as NodeJS.Timeout to resolve
const inpRef = useRef<NodeJS.Timeout | null>(null);
as per my knowledge set interwal will not work when user goes out of the browser window.
is it feasible to use setInterwal for a timer?
useEffect hook was missing
GoodJob!
thanks brother
how do you find these types? do we have a library for finding types when coding in typescript? TY!
Why useRef hook in setInterval? Is it a DOM element?
you are not showing output..
Can we use one ref for multiple input fields????
These vids are much underrated. I will forward this to every one I know !
Can't we use the setInterval method without the window object? I tried and typeScript gives an error, why? Can anyone explain, please?
awesome video so far. I noticed you saying this is the last hook (useRef), and I can see in the video a folder named "effect". So, I was just wondering if you have forgot to upload the video regarding useEffect. :/
when do you use useRef?
how to know the type of html element to pick it in type generic ?
👍 good job
Bro can you make Videos on Material UI?? There is no quality videos on Material UI. I hope you will work on it. And thanks for all of your amazing Tutorials.
When is ur next video cmg ,it's been 2 days we are waiting…
is this course complete? Can I start studying from here?
Hey, Vishwas … Can you make a Video series on Angular… There are no good Angular tutorials on youtube… Please provide us with some good Angular material.
please make video series on React testing
Hi Vishwas, thanks for sharing such wonderful knowledge with us. My Typescript Intellisence and auto-complete features are not working I have searched in google and tried something but still it is not working! I would really appreciate if you could help me to figure out the issue and get it working. Thanks in advance 🙂
Please upload faster sir