Trigger a Function when Scrolling to an Element in React with Intersection Observer

Sdílet
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

Komentáře • 343

  • @hamzaeshoul
    @hamzaeshoul Před 8 měsíci +6

    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.

    • @colbyfayock
      @colbyfayock  Před 8 měsíci +3

      thanks for the kind words!! glad it was helpful

  • @lostsunrises7495
    @lostsunrises7495 Před rokem +3

    Great video Colby! Super striaghtforward and easy to follow! This saved me from a big headache 🙌

  • @JamesQQuick
    @JamesQQuick Před 2 lety +16

    So cool and so easy!

  • @JatinS-yt
    @JatinS-yt Před rokem

    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!

  • @fidelisitor8953
    @fidelisitor8953 Před rokem +1

    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.

  • @loop8836
    @loop8836 Před 2 lety +5

    Man it’s 12:30am and I am figuring that shit from 11am from previous day
    And this is best video which explained perfectly

  • @John-eq5cd
    @John-eq5cd Před 6 měsíci +1

    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.

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

      thanks John! that sounds like a good solution. an infinite scroll video could be good 🤔😁

  • @Marlorouse109
    @Marlorouse109 Před rokem

    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.🙂👏

  • @zohabali7130
    @zohabali7130 Před rokem +1

    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

  • @colbyfayock
    @colbyfayock  Před 2 lety +6

    What's your favorite use case of scroll-based triggers? Reply 👇
    Make sure to subscribe for more! czcams.com/users/colbyfayock

    • @somerandomchannel382
      @somerandomchannel382 Před rokem

      is it possible to trigger many items with an classname instead of ref?

    • @colbyfayock
      @colbyfayock  Před rokem +1

      @@somerandomchannel382 sure is! check out this example: developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#javascript

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

    thanks for this video mate helps many people who had no idea about such observer hope your channel grows good .

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

    If i knew about this hook before it could have saved me so much heartache thank you for explaining this in a concise way.

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

      no problem! glad you were able to eventually find it :)

  • @silenux7419
    @silenux7419 Před 2 lety +6

    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.

    • @colbyfayock
      @colbyfayock  Před 2 lety +5

      thanks, no problem, definitely great to see both ways to have the option

  • @simple_user000
    @simple_user000 Před rokem

    Seriously bro, this is what i've been struggling for many days! Thanks a lot colby!!!

  • @w2ytube
    @w2ytube Před 2 lety

    Nice vid as usual. Very clear and very didactic 👏Thanks a lot Colby!

  • @athmanbakari9724
    @athmanbakari9724 Před rokem +1

    Great video, was really helpful
    It was nice how you explained each line with enough detail to grasp the concept

  • @anthonyiu
    @anthonyiu Před rokem

    That's brilliant! Thanks for the tutorial. That custom hook saved me a lot of time😊

  • @eleah2665
    @eleah2665 Před 2 lety +1

    Team! Thanks Colby. Always glad to see some npm package that does most of the work for me.

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

    This is what I needed for a sticky header changing style whilst scrolling. Thanks a lot

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

    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!

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

      no problem! glad you eventually found it :)

  • @mordicai4296
    @mordicai4296 Před rokem +1

    This is exactly what I was looking for, thank you!

  • @havefun5519
    @havefun5519 Před rokem

    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.🚀🚀

  • @RizaHariati
    @RizaHariati Před 2 lety

    Thank you! Your explanation is very simple and clear. And the hooks helps alot

  • @cvetanstojanovski559
    @cvetanstojanovski559 Před rokem

    Finally, I've understood what is useRef. Thanks a lot!

  • @vighneshs416
    @vighneshs416 Před rokem

    Amazing explanation Colby, you just earned a new sub. Thanks

  • @CryptoMarketSummary
    @CryptoMarketSummary Před rokem

    All of your videos have great little nuggets of "hey this is how useRef works" or other quirky functions/hooks/etc. Loving your videos.

    • @colbyfayock
      @colbyfayock  Před rokem

      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

    • @CryptoMarketSummary
      @CryptoMarketSummary Před rokem

      @@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'

  • @plorandi
    @plorandi Před 2 lety

    That was exactly what I was looking for. Thank you so much!

  • @totfosk
    @totfosk Před rokem

    Perfect video and explanation. I tried to do that on my own and was getting crazy getting undefineds everywhere XD. Cheers master

  • @8koi245
    @8koi245 Před rokem

    no way!! TYSM ❤️
    btw mixing this whit framer-motion is just *chef kiss*
    btw btw no way you actually talked about it!!

    • @colbyfayock
      @colbyfayock  Před rokem

      😂 similar minds! glad you enjoyed this :)

  • @adityatiwari7337
    @adityatiwari7337 Před rokem

    Thanks Colby! This was huge help!

  • @lucianalbuquerque
    @lucianalbuquerque Před 2 lety

    Perfect video, so clear! THANK YOU !

  • @trevorbryant2006
    @trevorbryant2006 Před rokem

    after many videos I have arrived to the one I need! Thanks for the awesome video man

  • @faustozambrano4901
    @faustozambrano4901 Před rokem +1

    Thanks a lot for this great video, brother...Imma get my pages wiggling so hard now

  • @umairkhalid8204
    @umairkhalid8204 Před rokem

    Thank you so much man! Just what I was looking for ❤

  • @rorymaguire145
    @rorymaguire145 Před 2 lety +1

    Great tutorial thank you Colby, helped me out a lot

  • @ranatrinchada
    @ranatrinchada Před rokem

    I just check it out! thank you so much for explaning this! 😎

  • @fabianpetersen2452
    @fabianpetersen2452 Před rokem

    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 👍

  • @biplabsharma5344
    @biplabsharma5344 Před rokem

    Subscribed mate amazing content

  • @good_hunter7724
    @good_hunter7724 Před rokem

    Spend a couple of hours before have find the answer in this video. Thanks!

    • @colbyfayock
      @colbyfayock  Před rokem

      wish you found it sooner :) but glad to hear that it helped!

  • @oswaldoj.avilesibarra3739

    Thanks Colby, just what I was trying to do. Suscribed!

  • @rodrigomenezes22
    @rodrigomenezes22 Před rokem

    Thank you man! This is awesome! I will start using it today! :)

    • @colbyfayock
      @colbyfayock  Před rokem

      no prblem! happy to hear it was helpful

  • @thelyricsguy3094
    @thelyricsguy3094 Před rokem +1

    Your explanation is very much clear and easy to understand 😃
    +1 subscriber

  • @inspriongaming4992
    @inspriongaming4992 Před rokem

    Thanks bro you explain much better and even saved my time

  • @sayedpritom
    @sayedpritom Před rokem

    Helped a lot! Thank you so much.

  • @Madmann16
    @Madmann16 Před rokem

    Just wow, i found another gem to the community thank you

  • @user-qs8rs1ul8l
    @user-qs8rs1ul8l Před 9 měsíci

    NIce! This is what I was looking for! Thank you

  • @ajjo5513
    @ajjo5513 Před 2 lety

    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.

    • @colbyfayock
      @colbyfayock  Před 2 lety

      no problem! Framer is an awesome library but there's a lot you can do before jumping on it

  • @reyreyalldayday5708
    @reyreyalldayday5708 Před 2 lety

    Colby yous the goat. Thanks for the vids dude

  • @Fishamble
    @Fishamble Před rokem

    Greta teaching style and wonderful presentation. Thanks.

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

    Many many thanks sir. Your tutorial really helps us :)

  • @M1szS
    @M1szS Před 3 měsíci +1

    thanks dude, this was rlly helpful

  • @deathdefier45
    @deathdefier45 Před rokem

    Top knotch content brother helped a lot at work thanks!

  • @YanParoni
    @YanParoni Před 2 lety +1

    you are amazing colby, thanks so much

  • @nocturno7887
    @nocturno7887 Před 2 lety

    Thank you man. I managed to implement your solution successfully

  • @atulyakr.2003
    @atulyakr.2003 Před 10 měsíci

    thank you very much. Bohot time bachaya h is trick ne mera🙏

  • @kunaldhuria3935
    @kunaldhuria3935 Před rokem

    What an amazing video thank you so much🙏

  • @abhishekkumar-kt3uk
    @abhishekkumar-kt3uk Před rokem +1

    Amazing, thanks buddy 🙏

  • @leonardocitton4627
    @leonardocitton4627 Před 2 lety +1

    Great content as ever!

  • @mohammadalaaelghamry8010

    Great video. Very useful thank you.

  • @aSTr0NeRF0999
    @aSTr0NeRF0999 Před 2 lety +2

    I like the way you teach !
    Great contents ! Thanks for sharing ! It's sooooo helpful ! +++

  • @felipespena1124
    @felipespena1124 Před rokem

    Great video, super clear! thanks

  • @alexmir9878
    @alexmir9878 Před rokem

    Great video and article, ty for it 🚀

  • @lauraneves9362
    @lauraneves9362 Před rokem

    Thanks!!! That was just what I needed!!

  • @jrome_20
    @jrome_20 Před 2 lety

    Super useful! Thank you so much!

  • @thedevmarc5634
    @thedevmarc5634 Před 2 lety

    Wow, this was so easy! Thank you!

  • @pouyabh
    @pouyabh Před rokem +1

    Thanks alooootttttttt
    For both ways that u said
    Love the session
    🔥🔥🔥🔥🔥🔥❤️❤️❤️❤️❤️❤️

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

    Really Helpful. Thanks!

  • @NotBeHaris
    @NotBeHaris Před 2 lety +1

    Thanks for sharing great knowledge.

  • @allanfernz9356
    @allanfernz9356 Před rokem

    Great video! You just gained a sub! :)

  • @davediaz25
    @davediaz25 Před rokem

    Man, you are a life saver!

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

    Thank you so much for this!!

  • @madsudan9227
    @madsudan9227 Před 2 lety +1

    Thanks for the superb explanation..keep teaching..

  • @nitinarora4315
    @nitinarora4315 Před rokem

    was a life-saver. Thanks a lot.

  • @colbyfayock
    @colbyfayock  Před rokem

    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

  • @laous
    @laous Před rokem

    Thank you very much Colby!

  • @johny962
    @johny962 Před 10 dny

    I learned something new. Thanks : )

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

    Very good video, helped me a lot!

  • @emirdior
    @emirdior Před rokem

    Great explanation, thanks

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

    Learned too much from this thank you

  • @juanortegaa6916
    @juanortegaa6916 Před 2 lety

    Hey Colby, just discovered your channel, but I know you from Major League Hackaton, Thanks for all these great content
    From Southamerica regards !!!

    • @colbyfayock
      @colbyfayock  Před 2 lety

      hey Juan thats awesome, thanks for checking out my channel!

  • @user-lg9il4ld1p
    @user-lg9il4ld1p Před 5 měsíci

    really understandble,thanks bro.

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

    Thank you so much Colby

  • @rohil3023
    @rohil3023 Před 2 lety

    Awesome tutorial!!

  • @developedbyvarun
    @developedbyvarun Před rokem

    Fantastic! You earned your subscription.

    • @colbyfayock
      @colbyfayock  Před rokem +1

      thank you!!

    • @developedbyvarun
      @developedbyvarun Před rokem

      @@colbyfayock btw do you mind telling me where to buy this T-shirt you are wearing?

    • @colbyfayock
      @colbyfayock  Před rokem +1

      @@developedbyvarun i got it on the GitHub Shop though im not seeing it: thegithubshop.com/

    • @developedbyvarun
      @developedbyvarun Před rokem

      Sad!

  • @ebertorresmacedo3667
    @ebertorresmacedo3667 Před rokem

    this video helped me alot!

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

    great example, thanks a lot

  • @imanshokryeh5406
    @imanshokryeh5406 Před rokem

    thx a lot it was very helpful👍

  • @madeit130
    @madeit130 Před rokem

    nice tutorial, thank you ;)

  • @patitorodri
    @patitorodri Před rokem

    Great, video!!

  • @manoj-k
    @manoj-k Před rokem

    Learn something whenever I come here❤

  • @lucasquadros5600
    @lucasquadros5600 Před 2 lety

    It helped me a lot, thx

  • @albinsjolin649
    @albinsjolin649 Před rokem

    Thank you my dude, was using some hack with onScroll and trying different hegihts and pixels.... THIS saved me

  • @kirakira160
    @kirakira160 Před 2 lety +1

    Thank you Colby

  • @xikmatillamominov8784

    great video thank you

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

    thx friend, you're a friend.

  • @ademferchichi1408
    @ademferchichi1408 Před rokem

    thank you .. you saved me !

  • @un9286
    @un9286 Před rokem

    subscribed my man

  • @onio907
    @onio907 Před rokem

    great video

  • @moretimeproductions
    @moretimeproductions Před 2 lety

    You are awesome!

  • @guy84eilon
    @guy84eilon Před rokem

    love it!