React Hooks useLayoutEffect Tutorial

  Рет қаралды 44,150

Ben Awad

Ben Awad

Күн бұрын

Learn how to use the useLayoutEffect hook in React.
Code: github.com/ben...
Links from video:
reactjs.org/do...
Playlist: • React Hooks Tutorial
----
If you like cooking, checkout my side project: www.mysaffrona...
----
Join the Discord: / discord
----
Patreon: / benawad
----
Follow Me Online Here:
Twitch: / benawad
GitHub: github.com/ben...
LinkedIn: / benawad
Instagram: / benawad97
Twitter: / benawad
#benawad
TikTok: / benawad ​

----
Follow me online: voidpet.com/be...
#benawad

Пікірлер: 52
@dremiq6670
@dremiq6670 4 жыл бұрын
To anyone who is struggling to understand the difference between useEffect and useLayoutEffect, it helped me a lot. useEffect (Component > State Changes > Component Renders > Rendered Component is Printed on Screen > useEffect runs) useLayoutEffect (Component > State Changes > Component Renders > useLayoutEffect runs > Rendered Component is Printed on Screen)
@romanbubnov5790
@romanbubnov5790 4 жыл бұрын
Oh, now it make sense, thank you!
@souravraychaudhuri6600
@souravraychaudhuri6600 3 жыл бұрын
Accurate 👌
@Ubuntu_amit
@Ubuntu_amit 3 жыл бұрын
State Changes as well first render, am i correct?
@souravraychaudhuri6600
@souravraychaudhuri6600 3 жыл бұрын
@@Ubuntu_amit yes... And uselayouteffect is synchronous. So, the final re-paint(after re-render) happens only after uselayouteffect has completed execution. (re-paint means when it's refreshed on the browser)
@dremiq6670
@dremiq6670 3 жыл бұрын
@@Ubuntu_amit idk what u mean by that but if Sourav's answer helps you there you go
@SuperNirupam
@SuperNirupam 5 жыл бұрын
Just the way hooks are meant to be used , fantastic work ! Your code Juggling is next label.
@benjamingeyer8907
@benjamingeyer8907 5 жыл бұрын
Thanks man, I will look at more of your hooks lessons
@freezemasterslick
@freezemasterslick 2 жыл бұрын
Awesome as usual
@kellinquinn7620
@kellinquinn7620 5 жыл бұрын
always hit the like button before watching
@Yetipfote
@Yetipfote 5 жыл бұрын
const BeforeWatchVideo = () => { useLikeButton() return }
@taimurazhar3923
@taimurazhar3923 5 жыл бұрын
Hey Ben, Great work as always. I wanted to pay you back by attracting a little more attention to your wonderful channel. I wanted to ask if you would mind if I took the learning from your videos and tried to make small tutorial articles on Medium. I will of-course attach full credit in these articles and link your channel as that is the main goal. If this is something that bothers you then please let know as I will of-course not start writing without your permission.
@bawad
@bawad 5 жыл бұрын
go for it
@rajbannasa7662
@rajbannasa7662 3 жыл бұрын
Thank you so much sir ❤️
@halfalligator6518
@halfalligator6518 3 жыл бұрын
Good lesson thanks. normally a functional component will re-render if its prop or a state has changed. Since we've offloaded this "useMeasure" hook to another file... how does the component know an important state has changed? Is it because within the render it refers to a value that useMeasure() returns? So maybe components re-render also when any value is returned from a hook? Maybe i should just take things for granted, but i get a bit stressed when too much magic is happening under the hood.
@aldeywahyuputra5719
@aldeywahyuputra5719 3 жыл бұрын
The useMeasure hook knows to re-render because we passed ‘deps’ as our input to it, which we use for useLayoutEffect as its dependency array.
@aqua123670
@aqua123670 5 жыл бұрын
Awesome !!
@teytag8844
@teytag8844 5 жыл бұрын
Dope, was doing this with useEffect cause I missed it in the docs, excited that layout exists. Do you have a recipe for checking the ref for existence (in case the getBoundingClientRect is running while the ref is unmounted) or would you just use a combination of your strategies mentioned in the other videos like the effect cleanup stuff?
@bawad
@bawad 5 жыл бұрын
I'm not sure about unmounted refs, I would probably put it in a component and do useEffect + useRef to see when the component is unmounting
@teytag8844
@teytag8844 5 жыл бұрын
@@bawad Gotcha, pretty much what I was thinking. Just for some context in case anyone else is curious about this, this question was born via a Tooltip component that shows up based on a trigger, and follows the mouse location for the duration that it's 'visible'. When it is unmounted, I noticed that some of the 'follow mouse' functions still run, assuming they have the data they expect, and since the Tooltip is unmounted things like ref are 'gone'.
@bawad
@bawad 5 жыл бұрын
I actually recently implemented something similar, I stopped listening for mouse events when the component unmounted with useEffect
@teytag8844
@teytag8844 5 жыл бұрын
@@bawad I was trying the same. The problem I ran into is that the eventListener handler that is set up when 'mounting' can continue to run after the event is removed via useEffect, which results in a 'trying to setState in unmounted component' no-op error. I have a sandbox set up with this if you're curious: codesandbox.io/s/event-handler-unbind-ir8td (move the mouse in and out of the pink box quickly, and you'll see the no-op error fire, oddly enough, just once. subsequent hovers don't cause it)
@teytag8844
@teytag8844 5 жыл бұрын
Another thing I noticed, if I set up my mouse event listener via a react `onMouseEnter/Leave` prop, this doesn't happen. I guess react cleans those up differently than if you set the listener outside of a component
@abdulbasir8740
@abdulbasir8740 2 жыл бұрын
thank you, it was wonderful, I also liked that custom hook you created namely `useFetch()`
@mylastore
@mylastore 4 жыл бұрын
Intellij, if you paste code from another JS file automatically imports all the local and npm modules for you :)
@patrickren7395
@patrickren7395 4 жыл бұрын
useMeasure is actually a hook from react-use
@shamilmammadov8246
@shamilmammadov8246 4 жыл бұрын
Great!
@nagendrannagendran9468
@nagendrannagendran9468 4 жыл бұрын
Hi ben. From this video, i only understood useeffect & useLayoutEffect are merely same but what's the difference between those. I've noticed an note which you mentioned at the start of this video. But for me it doesn't make sense and i think u'ven't tried to fully explain that in the rest of the video.
@viraj_singh
@viraj_singh 4 жыл бұрын
useLayoutEffect will execute synchronously after the dom is update so you can access dom elements inside useLayoutEffect but useeffect is not guaranteed to execute synchronously after dom updates so you can't access dom elements inside useEffect.
@drizzly_dips
@drizzly_dips 4 жыл бұрын
@@viraj_singh I don't think that's right. It says explicitly on the docs, "...useEffect is deferred until after the browser has painted, it’s guaranteed to fire before any new renders." reactjs.org/docs/hooks-reference.html#useeffect You can see in this Sandbox that useEffect and useLayoutEffect both execute identically (except, of course, that useLayoutEffect fires first) and both have access to DOM elements. codesandbox.io/s/acess-dom-elements-useeffect-uselayouteffect-wv69s?file=/src/App.js
@nugasoft
@nugasoft 5 жыл бұрын
Hi Ben, I need help!, I'm using: const [total, setTotal] = useState(0); and inside useEffect I run a function to get Total and I use setTotal(10), but my app, change again to 0 total, why!?? How I can run a function after load const [total, setTotal] = useState(0); ???
@bawad
@bawad 5 жыл бұрын
hard to say without seeing more code, put it in a codesandbox
@sandrosxila
@sandrosxila 3 жыл бұрын
7:32 dude [...deps]
@eat_mangos4177
@eat_mangos4177 5 жыл бұрын
good stuff, thanks
@tkdevlop
@tkdevlop 5 жыл бұрын
NEAT!!
@RohithVarma36
@RohithVarma36 3 жыл бұрын
Thanks, it’s good
@designer6219
@designer6219 4 жыл бұрын
which on is the useFetch video?
@bawad
@bawad 4 жыл бұрын
kzbin.info/www/bejne/oGK9g6ytbLSnkNU
@designer6219
@designer6219 4 жыл бұрын
@@bawad Thanks Ben
@alexn2566
@alexn2566 3 жыл бұрын
You should turn useMeasure into an open-source start-up and start do dev logs about it (ps: no hate, cool tutorial)
@keepforever726
@keepforever726 5 жыл бұрын
🤯
@qiroatmedia5444
@qiroatmedia5444 3 жыл бұрын
Abror qalesiz dosim )
@k4qdex
@k4qdex Жыл бұрын
web dev simplified explanation is way better.
@Jacur1980
@Jacur1980 4 жыл бұрын
🤔
@k4qdex
@k4qdex Жыл бұрын
u dont illustrate the reason u use it instead of useeffect.
@ericnyamu5249
@ericnyamu5249 5 жыл бұрын
too many hooks spoil the broth
@bawad
@bawad 5 жыл бұрын
😂
React Hooks useCallback Tutorial
11:15
Ben Awad
Рет қаралды 115 М.
React Hooks useEffect Tutorial
20:15
Ben Awad
Рет қаралды 157 М.
Hilarious FAKE TONGUE Prank by WEDNESDAY😏🖤
0:39
La La Life Shorts
Рет қаралды 44 МЛН
Thank you mommy 😊💝 #shorts
0:24
5-Minute Crafts HOUSE
Рет қаралды 33 МЛН
React Hooks useMemo Tutorial
11:21
Ben Awad
Рет қаралды 78 М.
When do you need React's useLayoutEffect hook?
8:23
Web Dev Cody
Рет қаралды 6 М.
React Hooks useContext Tutorial (Storing a User)
11:38
Ben Awad
Рет қаралды 358 М.
Golang: The Last Interface Explanation You'll Ever Need
17:58
Flo Woelki
Рет қаралды 25 М.
Learn React Hooks: useMemo - Simply Explained!
13:41
Cosden Solutions
Рет қаралды 118 М.
React Hooks useState Tutorial
15:31
Ben Awad
Рет қаралды 122 М.
Learn React Hooks: useLayoutEffect - Simply Explained!
9:17
Cosden Solutions
Рет қаралды 21 М.
Junior vs Senior React Folder Structure - How To Organize React Projects
16:16
Why I Love useReducer
22:40
Harry Wolff
Рет қаралды 92 М.
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 110 М.