Trigger a Function when Scrolling to an Element in React with Intersection Observer
Vložit
- čas přidán 27. 07. 2024
- Learn how to trigger functionality whenever someone scrolls to an HTML element in React with the browser's Intersection Observer API.
We'll walk through using the useRef hook to access a DOM node, using the Intersection Observer to detect if that element is visible inside of the useEffect hook, storing it's visibility in useState, using a hook library, and triggering CSS animations when scrolling down a page.
🧐 What's Inside
00:00 - Intro
00:13 - What tools we'll use like Intersection Observer and React
01:33 - Creating a new React app using a Next.js demo starter
03:01 - Using the useRef hook to access a reference to an HTML DOM node
03:38 - Accessing a Ref in React with useEffect
04:35 - Detecting HTML element visibility with Intersection Observer
06:24 - Storing element visibility state with the useState hook
07:57 - Installing React Intersection Observer hook to simplify code
09:23 - Triggering a CSS animation when scrolling to an element
11:59 - Outro
🗒️ Read More
spacejelly.dev/posts/how-to-t...
💾 Code
github.com/colbyfayock/my-scr...
🔔 Subscribe for more tech and developer videos
czcams.com/users/colbyfayock?s...
🐦 Get updates straight to your Twitter @colbyfayock
/ colbyfayock
📸 Catch the next stream live on Twitch @colbyfayock
/ colbyfayock
✉️ Or a newsletter right to your inbox!
www.colbyfayock.com/newsletter/
💝 Sponsor me for more free content like this!
GitHub: github.com/sponsors/colbyfayock
Other: hello@colbyfayock.com
👨🚀 Brought to by colbyfayock.com
www.colbyfayock.com
🎥 Want to know what A/V equipment I use?
www.colbyfayock.com/uses
🧰 More Resources
Demo Landing Starter
github.com/colbyfayock/demo-l...
Intersection Observer
developer.mozilla.org/en-US/d...
React Intersection Observer
github.com/thebuilder/react-i...
🎼 Music
Music from Uppbeat (free for Creators!):
uppbeat.io/t/eric-haley/lone-...
License code: LBXM57TITUFTCUND
#colbyfayock #intersectionobserver #reactjs #react #reactjstutorial #nextjs #javascript #webdevelopment - Věda a technologie
Very well explained. Thank you for your effort and your pedagogy. I particularly liked the smooth transition you made between using the intersection observer API and the react-intersection-observer package.
thanks for the kind words!! glad it was helpful
Great video Colby! Super striaghtforward and easy to follow! This saved me from a big headache 🙌
really glad to hear that!
So cool and so easy!
thanks James! 🙌
Man I can't thank you much, i have been stuck with the using intersection observer for weeks. That's a blessing man, Love you!
glad to hear it helped!! 🙌
Thanks for the video mate! Been struggling with this for a while now but this has helped me out a lot. Your explanation's really simple and easy to follow.
happy to hear this helped!
Man it’s 12:30am and I am figuring that shit from 11am from previous day
And this is best video which explained perfectly
Really glad to hear it helped!
Very clear, as with all of your videos, thanks.
I don't have a favourite use for Intersection Observer, but had been trying to apply it to infinite scrolling. Your video helped me to make the final step.
Using a hidden tag, such as a span, placed at the end of an array.map output, when the user scrolls down and reaches the span, inView becomes true and triggers a call to the database to retrieve additional array elements which are then displayed on the page.
thanks John! that sounds like a good solution. an infinite scroll video could be good 🤔😁
Thank you for taking the time to explain this functionality. and triggering concept. It really helps me to understand how to approach certain scrolling use cases in React.🙂👏
you're welcome!
wow!!! It was really smooth. Was looking for a solution like this for almost a day. You explained it in a perfect way. Thanks for providing so much value
Thanks glad to hear that!
What's your favorite use case of scroll-based triggers? Reply 👇
Make sure to subscribe for more! czcams.com/users/colbyfayock
is it possible to trigger many items with an classname instead of ref?
@@somerandomchannel382 sure is! check out this example: developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#javascript
thanks for this video mate helps many people who had no idea about such observer hope your channel grows good .
Thanks. 😁
If i knew about this hook before it could have saved me so much heartache thank you for explaining this in a concise way.
no problem! glad you were able to eventually find it :)
This was great.
Thanks for doing it the manual way first, sometimes we have restrictions on using third party packages so it's great to know how it works under the hood.
thanks, no problem, definitely great to see both ways to have the option
Seriously bro, this is what i've been struggling for many days! Thanks a lot colby!!!
No problem 🙌
Nice vid as usual. Very clear and very didactic 👏Thanks a lot Colby!
no problem! 🙌
Great video, was really helpful
It was nice how you explained each line with enough detail to grasp the concept
thank you! glad to hear that :D
That's brilliant! Thanks for the tutorial. That custom hook saved me a lot of time😊
Awesome! Thank you 😁
Team! Thanks Colby. Always glad to see some npm package that does most of the work for me.
definitely a useful one :)
This is what I needed for a sticky header changing style whilst scrolling. Thanks a lot
No problem!
Thank you so much! I had trudged through a bunch of very similar videos with people using scroll evenlisteners in react🤦♂before finding this one!
no problem! glad you eventually found it :)
This is exactly what I was looking for, thank you!
np, glad it helped!
I knew this NPM today, really coool~ it's good tut that you write the code first and then tried to use the NPM package to implement the same functionality.🚀🚀
thanks glad you found it helpful!
Thank you! Your explanation is very simple and clear. And the hooks helps alot
No problem 🙌 glad to hear that
Finally, I've understood what is useRef. Thanks a lot!
yay no problem!
Amazing explanation Colby, you just earned a new sub. Thanks
thank you!
All of your videos have great little nuggets of "hey this is how useRef works" or other quirky functions/hooks/etc. Loving your videos.
thanks! never thought about that perspective, but generally i like to give context around things to not make an assumption people know certain things :D
@@colbyfayock yeah man super appreciated. I feel like part of it is the fact that you explain even the "basic" stuff along the way. I.E., verbally saying "destructuring this import" while typing import { somethingCool } from 'superCool'
That was exactly what I was looking for. Thank you so much!
you got it! 🙌
Perfect video and explanation. I tried to do that on my own and was getting crazy getting undefineds everywhere XD. Cheers master
Thanks! Glad it helped
no way!! TYSM ❤️
btw mixing this whit framer-motion is just *chef kiss*
btw btw no way you actually talked about it!!
😂 similar minds! glad you enjoyed this :)
Thanks Colby! This was huge help!
glad to hear that!! 🙌
Perfect video, so clear! THANK YOU !
yay no problem!
after many videos I have arrived to the one I need! Thanks for the awesome video man
no problem :)
Thanks a lot for this great video, brother...Imma get my pages wiggling so hard now
Wiggle wiggle wiggle
Thank you so much man! Just what I was looking for ❤
no problem!
Great tutorial thank you Colby, helped me out a lot
No problem!
I just check it out! thank you so much for explaning this! 😎
no problem!
Thanks alot, i tried getting the window.scrollY to work with React and it is not as simple as the vanilla JS version. Your explanation was easy to follow, subscribed 👍
thanks, glad to hear that!
Subscribed mate amazing content
Spend a couple of hours before have find the answer in this video. Thanks!
wish you found it sooner :) but glad to hear that it helped!
Thanks Colby, just what I was trying to do. Suscribed!
Thank you!
Thank you man! This is awesome! I will start using it today! :)
no prblem! happy to hear it was helpful
Your explanation is very much clear and easy to understand 😃
+1 subscriber
Thank you!!
Thanks bro you explain much better and even saved my time
Glad to hear it helped!
Helped a lot! Thank you so much.
no problem!
Just wow, i found another gem to the community thank you
NIce! This is what I was looking for! Thank you
glad you found it! 🙌
Thank you so much. Always wanted to know how to use intersection observer in react and how such animations worked in react without reaching for a library like framer motion.
no problem! Framer is an awesome library but there's a lot you can do before jumping on it
Colby yous the goat. Thanks for the vids dude
thank you 🙏
Greta teaching style and wonderful presentation. Thanks.
thanks and your welcome!
Many many thanks sir. Your tutorial really helps us :)
You're welcome!
thanks dude, this was rlly helpful
no problem!
Top knotch content brother helped a lot at work thanks!
thank you!
you are amazing colby, thanks so much
You got it!!
Thank you man. I managed to implement your solution successfully
awesome! glad it worked out 💪
thank you very much. Bohot time bachaya h is trick ne mera🙏
No problem!
What an amazing video thank you so much🙏
no problem :D
Amazing, thanks buddy 🙏
No problem 🙌
Great content as ever!
thanks :D
Great video. Very useful thank you.
You're welcome!
I like the way you teach !
Great contents ! Thanks for sharing ! It's sooooo helpful ! +++
Thank you! No problem at all 🙌
x2
Great video, super clear! thanks
thank you!
Great video and article, ty for it 🚀
no problem!
Thanks!!! That was just what I needed!!
No problem 🙌
Super useful! Thank you so much!
No problem! 🙌
Wow, this was so easy! Thank you!
No problem 🙌
Thanks alooootttttttt
For both ways that u said
Love the session
🔥🔥🔥🔥🔥🔥❤️❤️❤️❤️❤️❤️
Glad it helped!
Really Helpful. Thanks!
no problem!
Thanks for sharing great knowledge.
no problem! 🙌
Great video! You just gained a sub! :)
thank you!!
Man, you are a life saver!
i got you!
Thank you so much for this!!
no problem!
Thanks for the superb explanation..keep teaching..
No problem 😁🙌
was a life-saver. Thanks a lot.
No problem 🙌
Dig deeper into React with my course Full Stack React with Appwrite: spacejelly.dev/reactappwrite
Otherwise get fresh tutorials and other free content straight to your inbox! colbyfayock.com/news
Great video!!
@@westbrook0853 thank you 🙌
Thank you very much Colby!
no problem!
I learned something new. Thanks : )
no problem!
Very good video, helped me a lot!
thanks! glad to hear that
Great explanation, thanks
No problem! 🙌
Learned too much from this thank you
no problem!
Hey Colby, just discovered your channel, but I know you from Major League Hackaton, Thanks for all these great content
From Southamerica regards !!!
hey Juan thats awesome, thanks for checking out my channel!
really understandble,thanks bro.
no problem!
Thank you so much Colby
no problem!
Awesome tutorial!!
Thank you!!
Fantastic! You earned your subscription.
thank you!!
@@colbyfayock btw do you mind telling me where to buy this T-shirt you are wearing?
@@developedbyvarun i got it on the GitHub Shop though im not seeing it: thegithubshop.com/
Sad!
this video helped me alot!
awesome to hear!
great example, thanks a lot
no problem!
thx a lot it was very helpful👍
Np!
nice tutorial, thank you ;)
no problem!
Great, video!!
Thank you!
Learn something whenever I come here❤
awesome happy to hear that! 🙌
It helped me a lot, thx
Awesome, no problem!
Thank you my dude, was using some hack with onScroll and trying different hegihts and pixels.... THIS saved me
glad to hear that! 🙌
Thank you Colby
you're quite welcome! :)
great video thank you
no problem!
thx friend, you're a friend.
nada 😁
thank you .. you saved me !
yay! np 🙌
subscribed my man
thank you!! 🙌
great video
thank you!
You are awesome!
thank you! 🤩
love it!
thank you!