React Hooks useRef Tutorial

  Рет қаралды 97,540

Ben Awad

Ben Awad

Күн бұрын

Пікірлер: 61
@jrs_devs
@jrs_devs 4 жыл бұрын
I was not understanding most part of your videos as I was watching them being a total beginner to React, but I've come here after a month and they are very clarifying. Thank you for taking your time on doing this.
@aashayamballi
@aashayamballi 5 жыл бұрын
Thank you so much, it was so clear and concise. Thank you again :)
@madebylewis
@madebylewis 5 жыл бұрын
You can also utilise a hook with useRef to return a previous value (e.g. prevProps, prevState), pretty neat!
@hemantjain761
@hemantjain761 4 жыл бұрын
Can you share some examples?
@xerxius5446
@xerxius5446 4 жыл бұрын
@@hemantjain761Go to React Docs's FAQs page, this hook's code is there
@dawid_dahl
@dawid_dahl 3 жыл бұрын
The final part of the video was gold for me, I’m working with some component transition animations and I have that error. Thanks!
@joshpeterson1004
@joshpeterson1004 4 жыл бұрын
If you pass [] into useEffect and then set isCurrent.current to false inside, why isn't it set to false on first render?
@tslqs
@tslqs 5 жыл бұрын
Waw I did not know useRef is this good, thanks!
@AbhishekSharma-mm6ld
@AbhishekSharma-mm6ld 3 жыл бұрын
Thanks for highlighting all those things with useRef most of the time I only use it for dom reference only. But Even though as per the last part I would have still gone for clearTimout during the memory clean up :)
@yuushamenma2130
@yuushamenma2130 2 жыл бұрын
On line 12 of your useFetch hook: Why do we use a callback of setState(state => ({...})) instead of just doing setState({data: state.data, loading: true}) ?
@lilibayo
@lilibayo 5 жыл бұрын
For the isCurrent example to ensure we dont call setState on an unmounted component, I have seen some examples that utilizes useState with a variable isMounted. Everything is pretty much the same as the example you provided. Curious as to the difference in using state vs ref to keep track of the isMounted/isCurrent state/var. Is one better or is it just a personal pref? Thanks!
@JagaSantagostino
@JagaSantagostino 5 жыл бұрын
for isMounted using state with force an additional rerender due to the state changing, updating ref.current will not trigger a rerender. Internally useref uses usestate, this is why you pass the entire ref to the component so that react can change it for you This is very informative: www.reddit.com/r/reactjs/comments/aufijk/useref_is_basically_usestatecurrent_initialvalue_0/ More info: reactjs.org/docs/hooks-reference.html#useref
@lilibayo
@lilibayo 5 жыл бұрын
@@JagaSantagostino ah, makes sense. Thanks for the explanation
@pratikkamath8423
@pratikkamath8423 3 жыл бұрын
Great video Ben! Pretty concise and well-explained :)
@kirankandula2779
@kirankandula2779 4 жыл бұрын
Thanks for the video. The usecases considered here are very helpful.
@Seb16291629
@Seb16291629 5 жыл бұрын
Clear and concise ! Time for some refactoring !
@hashcode_01
@hashcode_01 2 жыл бұрын
Hey Ben, well done, good use cases, just to let everybody knows, the unmount warning use case is resolved in React 18 , no use case anymore so.
@herrklaus
@herrklaus 3 жыл бұрын
Thank you, Ben.
@ZhDinerstein
@ZhDinerstein 5 жыл бұрын
Love these videos! Precise and very easy to follow. If possible, please increase the zoom on your IDE in the future. Can be difficult to read sometimes while viewing on a phone.
@bawad
@bawad 5 жыл бұрын
Sure
@GlenBondMogane
@GlenBondMogane 4 жыл бұрын
I've learnt alot to this Playlist... Very well explained and great examples for all use cases
@ateebahmed2237
@ateebahmed2237 4 жыл бұрын
Mast video banai h, maza agya !
@Ko0lHaNDLuKex
@Ko0lHaNDLuKex 5 жыл бұрын
Hi Ben, thanks again for an informative video! A question: what's stopping me using a plain JS variable to ensure the state isn't being updated after unmounting? Like, just saying `let isMounted = true` when the effect is called, checking isMounted before setting any state, and then setting isMounted to false in the cleanup function? Why do we have to use a ref here? Thanks!
@bawad
@bawad 5 жыл бұрын
If you put that variable inside the function it will reset every render, useRef lets you persist the value between renders
@petersantoso2605
@petersantoso2605 5 жыл бұрын
@@bawad A question: wouldn't it the same, even the var isMounted reset to true it will set to false in cleanup function? so the setState wont be called.
@bawad
@bawad 5 жыл бұрын
Yeah, but if a variable should be scoped with the component then it's better not to make it global
@petersantoso2605
@petersantoso2605 5 жыл бұрын
@@bawad got it, thank you for your reply!
@shankar99977
@shankar99977 Жыл бұрын
​​@@bawad Quick question: Insteading taking useref value as a boolean, taking it as a null initially and passing a setTimeout reference to it and clearTimeout in useeffect return for that ref would also do the same thing, isn't?
@javierzapien314
@javierzapien314 4 жыл бұрын
I don’t see how the hello component is re-rendering as you type??
@chrisarcher9907
@chrisarcher9907 4 жыл бұрын
any thoughts on how to provide focus on mapped inputs?
@何家華-l1b
@何家華-l1b 5 жыл бұрын
awsome,useful,perfect,Thank you!
@the_derpler
@the_derpler 4 жыл бұрын
Thanks. This is a good video. It helped me clear up some misconceptions I had about refs.
@vaasudhand3200
@vaasudhand3200 3 жыл бұрын
For the second useCase, can't we just use a "let variable" to maintain some data across re-renders. Obviously that variable will not be used anywhere, where it can cause a re-render?
@hashcode_01
@hashcode_01 2 жыл бұрын
it will be redefined in each re-render
@rohandevaki4349
@rohandevaki4349 4 жыл бұрын
your code on github seems like it is the finished code, but not initial code. please provide the initial code
@oorangecchicken
@oorangecchicken 5 жыл бұрын
Interesting... but how does useRef(0) not reset back to “0” every time the component re-renders? Shouldn’t it always render “1” even with the unary (++) operator?
@bawad
@bawad 5 жыл бұрын
the value of useRef only resets when the component is unmounted
@AnilPai
@AnilPai 5 жыл бұрын
Thank you so much for your react hooks tutorial videos. I stumbled upon your video when I was searching for useRef usage, looks like you have the best explanation of all. Including all the different use cases. I would suggest you to update the React docs with your examples.
@mayankketkar3450
@mayankketkar3450 4 жыл бұрын
What does imperative calling imply? I get the definition however i am trying to distinguish that from using props to propagate changes
@bawad
@bawad 4 жыл бұрын
props propagate, imperative does not
@DiogoLessa
@DiogoLessa 5 жыл бұрын
Hi, Ben! Could you share with us some extensions you use in vscode to speed up your coding? I can see you have some cool stuff around... We'd appreciate!
@bawad
@bawad 5 жыл бұрын
here are the extensions I use: benawad.com/plugins The speed comes from using vim though benawad.com/vim
@tannerbyers6729
@tannerbyers6729 4 жыл бұрын
Awesome tutorial! Really helped! Subscribed :)
@kishorrathva4097
@kishorrathva4097 4 жыл бұрын
in my case 'Hello render ' console twice every time i type like if i type 'a' it console 'Hello render 1' and Hello render 2' , can any one help?
@arjunprakash2815
@arjunprakash2815 4 жыл бұрын
May be try after removing of strict mode in Index .js file. Note: In dev mode you can comment or remove strict mode but make sure for prod you can use strict mode. Hence it render only once in prod rather than dev. Eg: import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( // , // , document.getElementById('root') );
@dericbytes
@dericbytes 4 жыл бұрын
Really well done. Thanks
@pjately078
@pjately078 5 жыл бұрын
Do you have any tutorials on creating custom hooks like the useFetch hook in this video?
@bawad
@bawad 5 жыл бұрын
Not specifically
@vasupatel6932
@vasupatel6932 5 жыл бұрын
Short and Sweet
@zhanghedev
@zhanghedev 5 жыл бұрын
I am the first one to support you.
@tomermatmon
@tomermatmon 5 жыл бұрын
Great! Thank you!
@kareemsakr41
@kareemsakr41 5 жыл бұрын
Great work. keep it up.
@manthenagowtham9437
@manthenagowtham9437 4 жыл бұрын
I have a code which is multiple popover in material ui in react class components. I need that code to be in react hooks. Can you help me? If so please reply me I'll send the code link.
@phantazzor
@phantazzor 3 жыл бұрын
nice , you going really fast but nice video
@julianb.8749
@julianb.8749 4 жыл бұрын
deep!
@adrisongomez8454
@adrisongomez8454 3 жыл бұрын
That moment when you realize that you are not the only one that feel loose trying copy and paste using VIM in a Mac.
@aqua123670
@aqua123670 5 жыл бұрын
thank you
@zlackbiro
@zlackbiro 5 жыл бұрын
Yes, React users forgets JavaScript very quick... 🙂
@raptus9115
@raptus9115 4 жыл бұрын
Sorry, but ref's is the worst idea the React team has come up with, so what if I need to detect the widths and heights of 20 elements? I have to add refs for 20 elements? Insanity. I love React but anything relating to DoM manipulation quickly becomes a clusterfuck of ref's and is tragically unmaintainable. A better solution needs to be on the table.
@CaioUechi
@CaioUechi Жыл бұрын
damn, you are too fast. I guess Im too dumb for this hook atm lol
React Hooks useLayoutEffect Tutorial
10:22
Ben Awad
Рет қаралды 44 М.
React Hook useRef and forwarding refs with forwardRef
14:46
techsith
Рет қаралды 81 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
Learn useRef in 11 Minutes
10:20
Web Dev Simplified
Рет қаралды 654 М.
React Hooks useEffect Tutorial
20:15
Ben Awad
Рет қаралды 157 М.
How I Made a Game in React Native
12:01
Ben Awad
Рет қаралды 48 М.
React Hooks useMemo Tutorial
11:21
Ben Awad
Рет қаралды 78 М.
Why I Love useReducer
22:40
Harry Wolff
Рет қаралды 92 М.
React Hooks: Advanced Hooks
20:52
Harry Wolff
Рет қаралды 87 М.
Learn useEffect In 13 Minutes
13:38
Web Dev Simplified
Рет қаралды 854 М.
What Programming Font Should You Use?
4:12
Ben Awad
Рет қаралды 48 М.
How to Roll Your Own Auth
13:05
Ben Awad
Рет қаралды 124 М.
React Hooks useContext Tutorial (Storing a User)
11:38
Ben Awad
Рет қаралды 358 М.