Learn React Hooks: useRef - Simply Explained!

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • 🚀 Project React → cosden.solutio...
    📥 Import React (Newsletter) → cosden.solutio...
    Join The Discord! → discord.cosden...
    Source Code → github.com/cos...
    In this video we will learn about React hooks, starting with useRef. This powerful React hook will allow you to store and manipulate values that are not needed for rendering. You will learn the difference between useRef and useState, when to use which, and how useRef works under the hood. We will use refs to handle values without triggering re-rendering, and we will also look at how refs can be used to manipulate HTML elements, as well as how they are used in 3rd party libraries.
    In this new React world, hooks are here to stay, so it's best to learn them! In this tutorial I demonstrate the useRef React hook, and I explain it very simply and in a way that is easy to understand. Enjoy!

Komentáře • 185

  • @cosdensolutions
    @cosdensolutions  Před 5 měsíci +6

    Hope you enjoyed the video! I also have a really cool and unique course that will teach you way more than this video. You'll learn how to build an actual complex project with React. It's called "Project React" and you can find it at cosden.solutions/project-react. Also, I have a free weekly newsletter called "Import React" with tutorials, news, and cool stuff about React! You can sign up at cosden.solutions/newsletter?s=ytc

  • @davidkurniawan1358
    @davidkurniawan1358 Před 8 měsíci +16

    This is the reason why it's hard to understand about useRef. It's so confusing but you help me to make it easier to understand. GREAT JOB!!!

  • @psychobuddha5379
    @psychobuddha5379 Před rokem +8

    I was bit suspicious when you said we will not need another video for this but you were right. Really nice explanation, I finally understand the usage of refs.

  • @dirtysouth3291
    @dirtysouth3291 Před rokem +31

    This is the BEST useRef tutorial - you are VERY good at explaining things in a decent amount of time, and you cover everything. I will try to find more useful videos on your channel, hopefully, there will also be something about TypeScript

  • @TheGr33k13
    @TheGr33k13 Před rokem +17

    Great job! Your videos have been really clear and provide straight forward examples even explaining some of the gotchas of react

  • @rustam199639
    @rustam199639 Před 2 měsíci

    Wow... I spent hours on it, when I used states and didn't understand why its not changing it immediately... So grateful !
    Thank you man!

  • @katadermaro
    @katadermaro Před rokem +4

    Finally, I understood useRef. You explain very well my friend. Thank you!

  • @Pernicuz
    @Pernicuz Před 7 měsíci +3

    These guides are incredible. You made me understand clearly in 3 videos what my teacher couldnt in 3 days

  • @erickchavez4551
    @erickchavez4551 Před rokem +9

    I think I'm starting to get this hook. Easy to follow examples 👍. Also, it's great that you use the dark mode, my eyes appreciate it.

  • @woodzeppelin3241
    @woodzeppelin3241 Před 2 dny

    A really good deep dive on hooks for me. Thanks

  • @ZahidHussain-hj2dk
    @ZahidHussain-hj2dk Před 9 měsíci +2

    Best useref tutorial.....highly recommended

  • @mostinho7
    @mostinho7 Před 7 měsíci +1

    useRef stores state that doesn’t trigger a rerender when it changes. The useRef value isn’t used in the jsx component
    2:45 nuance about setState, when you call setState to set a new state value, that state variable which will not have the same value until the next render, but incrementing a useRef value is immediate, happens right away not in the next render because useRef doesn’t even trigger a rerender
    8:20 so usually you don’t use useRef value in the jsx because your component won’t rerender to display the latest useRef value, but you can use useRef to hold reference to HTMLElement to access dom elements directly and call functions on them like focusing a component

  • @user-og5rw1nk7d
    @user-og5rw1nk7d Před rokem +2

    Thanks, you explained it in a very simple way👌. I want to learn Express from you.

  • @Engr.SharoonHoney
    @Engr.SharoonHoney Před 3 měsíci

    That's amazing explanation of useRef on whole CZcams. Thanks sir 👍

  • @SK-fq2yz
    @SK-fq2yz Před 7 měsíci +1

    Thank you very much. It really is the last useRef video i would ever need.

  • @official_youtube_1
    @official_youtube_1 Před rokem +3

    great explanation, Exactly what I was looking for
    It clears many doubt about state vs ref.
    count a like from me.

  • @buildervision7082
    @buildervision7082 Před 18 dny

    awesome I now understand . Thanks for the explanation. i'm looking forward to watching more of your videos

  • @haciendadad
    @haciendadad Před 11 měsíci +2

    I really like this description, it made sense. I am a new sub and opted to be notified of all your videos. You seems like this is a good example of the new gen of code videos.

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

    You're the type of tutor we just want to hit that like button for without first having to watch the video, because previous videos were on point!
    Thanks!
    + would you also mind if I were to explain these hooks but in other language with credit like "Inspired by: Cosden Solutions"?

  • @julienheng3880
    @julienheng3880 Před 8 měsíci

    you did an excellent job in explaining things with examples in short amount of time. Give me more tutorials lol

  • @melikarazavi3599
    @melikarazavi3599 Před rokem

    useRef is very complicated for me , you make it simple .Thank you a lot!

  • @pranavjaiswal2379
    @pranavjaiswal2379 Před rokem

    Thanks for the clarification i really understood the difference between the useref and usestate you made my 12 minutes worth

  • @CodingWithDapo
    @CodingWithDapo Před rokem

    Thank you so much, I have wasted so much time searching for this simple answer.

  • @adrianayon5513
    @adrianayon5513 Před 10 měsíci +2

    Thank you, very good explanation!! You helped me a lot!!

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars Před 10 měsíci +2

    wow, simple and to the point
    thanks a lot :)

  • @alejandroluisacosta2164
    @alejandroluisacosta2164 Před hodinou

    Nice example. Thanks

  • @morofernando639
    @morofernando639 Před 9 měsíci

    Absolutely another wonderful tutorial after I watched this video I can say that I am super confident with working with useRef your explanation is really straightforward and clear

  • @kasiakordys266
    @kasiakordys266 Před rokem

    I have watched it 3 times and finally got it :-) thanks, great explanation

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

    This a good explanation of useRef.
    However, there is an issue when using it when Concurrent Mode is activated in React.
    That's a topic that is even more complex in regards to the usage of useRef.
    I don't know if you can make a video on but it would help people who are going to run into that issue sooner or later. Thanks

  • @jamilibrahimli190
    @jamilibrahimli190 Před 7 měsíci

    Thank you so much , i was realy confused when read the docs , but now it's clear much more better!

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

    Earned a subscribe! Thank you for explaining this super clearly and explicitly.

  • @baciusebastian6361
    @baciusebastian6361 Před 10 měsíci +1

    really good explanation, thank you for the video, really appreciate it

  • @vesa95
    @vesa95 Před rokem +3

    Love this series!!! 🤗

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

    love your explanation. every thing is clear now about useRef, thanks!!

  • @cheiguerabdelwehab6802
    @cheiguerabdelwehab6802 Před 9 měsíci

    Thank you so much, this video helped me in my interview, specially the re-rendering part

  • @vishalupadhayay6391
    @vishalupadhayay6391 Před rokem +3

    Nice video and appreciate the time and effort that you put into developing quality content.

  • @pp-studio
    @pp-studio Před 5 měsíci

    Thank you so much for your incredible explained for react hook. it 's make me supper clear. 🥰

  • @wintersol9921
    @wintersol9921 Před 7 měsíci

    This video not only made me understand useRef better, you just made me realize how react re-renders work :D Thanks man.

  • @altan183
    @altan183 Před 6 měsíci

    Very clear and concise, thank you for the explanation.

  • @arushraj3807
    @arushraj3807 Před 9 dny

    Amazing explanation 🎉

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

    Loved this series man !!
    btw whats the vscode theme ?

  • @user-yx4xd7zs1d
    @user-yx4xd7zs1d Před měsícem

    Amazing, I enjoy watching your React lectures!

  • @vincetamayo9669
    @vincetamayo9669 Před 6 měsíci

    very clear and concise tutorial. thanks for this content

  • @eliyahutarab4862
    @eliyahutarab4862 Před rokem +1

    Clear and simple thank you

  • @zambidzhelov6734
    @zambidzhelov6734 Před rokem

    This is the best useRef tutorial . Thansk !

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

    To elaborate 2:24 useRef() always returns an object with one 1 property, literally 1 called 'current'. If you console.log(ref), you will see this and 'current' is of 'any' type.

  • @andrewdatar9880
    @andrewdatar9880 Před 9 měsíci

    Your reach hooks playlist is awesome. Thank you. As for useRef vs useState - I understand the point you are trying to show. But you kinda comparing apples to oranges. If you make your state object (to be similar to ref shape) - then they are exactly the same, and state is updated in current render too, just like ref!
    const [count, setCount] = useState({value: 0});
    // click
    count.value++;
    setCount({ value: count.value });
    countRef.current++;
    console.log(count.value, countRef.current); // both are 1 after first click!
    or even simpler. Just do to the count what you do to the current:
    let [count, setCount] = useState(0);
    // click
    count++;
    setCount(count);
    countRef.current++;
    console.log(count, countRef.current); // both are 1 after first click!

    • @cosdensolutions
      @cosdensolutions  Před 9 měsíci +1

      Sure they might behave the same, but updating state like that won't trigger a re-render of the component so you don't want to do that. If you do then use a ref because that's what it's for. I think the explanation made sense because yes they are different, but serve different purposes

    • @andrewdatar9880
      @andrewdatar9880 Před 9 měsíci

      @@cosdensolutionsSure, your explanation makes perfect sense. It's react API that is too complex and requires explanations like this. And it allows to be misused like in my made up example, and yet it still re-renders and works properly. Even though in my example value of count is changed instantly (just like ref) and not in next re-render. And, yes, it triggers re-render.

  • @ahmednabeel358
    @ahmednabeel358 Před 11 měsíci

    You will be famous one day keep going 🙂 thanks a lot

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

    Pls do a series on react beginners completing all concepts

  • @rushikeshjadhav7748
    @rushikeshjadhav7748 Před rokem

    Hats off to you sir! Great explanation for a beginner like me

  • @samuelerbo
    @samuelerbo Před 8 měsíci

    Thanks man! I learned a lot.

  • @pravinharigaikwad
    @pravinharigaikwad Před rokem +1

    Great explanation 👏

  • @user-jn2rs3rw3x
    @user-jn2rs3rw3x Před 8 měsíci

    now I cant forget this, tq!

  • @syuo5051
    @syuo5051 Před 3 měsíci

    Many thanks for this clear explanation. 💗💗

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

    Very nice explained

  • @suhaibsaifan7163
    @suhaibsaifan7163 Před rokem

    Great explanation! .Looking forward for more series

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

    Bro great explanation. Thanks

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

    useRef is used for storing a DOM element (i.e. it's REFerence) or values that are not reactive.

  • @starfinitex
    @starfinitex Před 6 měsíci

    I really enjoyed it and understood

  • @nodnik7
    @nodnik7 Před 10 měsíci

    Ho Wow. at 6 min it clear my mind.
    awesome, thanks.

  • @user-hl9us2id5i
    @user-hl9us2id5i Před 8 měsíci

    Best explanation 🎉

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

    Thanks. But i still need time to pondering about this hook.😊

  • @nowarm
    @nowarm Před 2 měsíci

    love your explanation!

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

    The best explanation! Thank you

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

    Good explanation, thanks

  • @saschagobbels2177
    @saschagobbels2177 Před 10 měsíci

    absolutely great explanation ! thx my friend

  • @lhommeunknown3985
    @lhommeunknown3985 Před rokem

    Great video man, really clear explanations !

  • @shlomov
    @shlomov Před 12 dny

    Your videos are great
    I just don't understand what is the difference between a regular variable and a useRef. they both don't re-render. I can just let var = 0 and then use it wherever I want

  • @OmG21097
    @OmG21097 Před 7 měsíci

    Thanks for detailed explanation!!

  • @TheRealMinhHoang
    @TheRealMinhHoang Před rokem

    Thank you for creating an amazing tutorial, it's truly help me a lot!
    I have a request, can you make video about using useRef, forwardRef and useImperativeHandle hook in React? Component that managing it's own state. I have always wanted to know more about this react pattern.

  • @AhammedShaheedhudheen
    @AhammedShaheedhudheen Před 8 měsíci

    you Earned a subscription

  • @roan6903
    @roan6903 Před 11 měsíci

    Very much thanks about the userState rendering. It bothers me for a long time.

  • @polireddyrajulanaidu9032
    @polireddyrajulanaidu9032 Před 8 měsíci

    Can you make a video on how to use refs in autocomplete material ui component

  • @tombrady7390
    @tombrady7390 Před 11 měsíci

    when you said wont need to watch a useRef video again you were ont kidding.

  • @petrus2746
    @petrus2746 Před 7 měsíci

    Nicely done my man.

  • @mohit6847
    @mohit6847 Před 10 měsíci

    Thanks for the video, it was a good explanation.

  • @esraamohamed5601
    @esraamohamed5601 Před 8 měsíci

    Keep doing the best work.

  • @saadchraibi6712
    @saadchraibi6712 Před rokem

    Awesome, straight to the point!
    Could you make a video about custom Hooks?

    • @cosdensolutions
      @cosdensolutions  Před rokem +1

      Already have ☺️

    • @saadchraibi6712
      @saadchraibi6712 Před rokem

      @@cosdensolutions would you mind sharing the link? I can’t find it

    • @cosdensolutions
      @cosdensolutions  Před rokem

      @@saadchraibi6712 check my channel -> playlists -> custom hooks (there's only one video but there will be more)

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

    Helpful information thank you

  • @codelivewithme
    @codelivewithme Před 7 měsíci

    much better then so called "paid courses"

  • @idfk2873
    @idfk2873 Před rokem

    its very healful..great Video

  • @supplyuniquenotessun4562
    @supplyuniquenotessun4562 Před 11 měsíci

    respected .. very thanks

  • @blackfortmusic7729
    @blackfortmusic7729 Před rokem

    Growing fast man!

  • @asifmondal7609
    @asifmondal7609 Před 9 měsíci

    Well explained buddy.. Big 👍

  • @leebobtheblob87
    @leebobtheblob87 Před rokem

    6:52 UseRef vs useState : triggers render?
    8:29
    Using ref on element
    Etc:
    - hooks runs after render

  • @fatemeh-azad
    @fatemeh-azad Před 5 měsíci

    Thanks man this is awsome

  • @ajays3914
    @ajays3914 Před 10 měsíci

    Awesome video brother 😎

  • @dejandjosic1258
    @dejandjosic1258 Před rokem

    Very well explained. Subscribed.

  • @sankalpsharma3050
    @sankalpsharma3050 Před 7 měsíci

    Thanks Cosden . Good video

  • @someChicoRy
    @someChicoRy Před rokem

    круто, продолжай
    its intresting.
    continue This)

  • @samahgad241
    @samahgad241 Před 8 měsíci

    Realy, thanks alot😍

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

    Very useful

  • @rennjaysoterio2543
    @rennjaysoterio2543 Před 10 měsíci

    Great explanation!

  • @akankshagunti5301
    @akankshagunti5301 Před 7 měsíci

    Loved it 💌

  • @chadaramprasanna9259
    @chadaramprasanna9259 Před 7 měsíci

    Thanks bro. 💯

  • @AhmedHuzain
    @AhmedHuzain Před 8 měsíci

    Thank you!

  • @malayalenka9164
    @malayalenka9164 Před 9 měsíci

    Awesome!

  • @pasinduvinsuka
    @pasinduvinsuka Před 9 měsíci

    Thank you so much

  • @nabilachehlafekir8577
    @nabilachehlafekir8577 Před 11 měsíci

    thank you so much ^^

  • @jay-kv6wn
    @jay-kv6wn Před rokem

    Great video as always