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

Поделиться
HTML-код
  • Опубликовано: 2 июн 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
    ruclips.net/user/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
  • НаукаНаука

Комментарии • 340

  • @hamzaeshoul
    @hamzaeshoul 6 месяцев назад +4

    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  6 месяцев назад +3

      thanks for the kind words!! glad it was helpful

  • @lostsunrises7495
    @lostsunrises7495 Год назад +3

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

  • @fidelisitor8953
    @fidelisitor8953 Год назад +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.

  • @Marlorouse109
    @Marlorouse109 Год назад

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

  • @John-eq5cd
    @John-eq5cd 4 месяца назад +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  4 месяца назад

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

  • @loop8836
    @loop8836 Год назад +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

    • @colbyfayock
      @colbyfayock  Год назад +1

      Really glad to hear it helped!

  • @ashimxtha6407
    @ashimxtha6407 8 месяцев назад +1

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

  • @zohabali7130
    @zohabali7130 Год назад +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

  • @silenux7419
    @silenux7419 2 года назад +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  2 года назад +5

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

  • @adityatiwari7337
    @adityatiwari7337 Год назад

    Thanks Colby! This was huge help!

  • @athmanbakari9724
    @athmanbakari9724 Год назад +1

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

  • @w2ytube
    @w2ytube 2 года назад

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

  • @JatinS-yt
    @JatinS-yt Год назад

    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!

  • @KingPacavision
    @KingPacavision 8 месяцев назад +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  8 месяцев назад

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

  • @marktheunknown1829
    @marktheunknown1829 25 дней назад

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

  • @RizaHariati
    @RizaHariati Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад

      No problem 🙌 glad to hear that

  • @_aditya_giri_
    @_aditya_giri_ Год назад

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

  • @anthonyiu
    @anthonyiu Год назад

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

  • @mordicai4296
    @mordicai4296 Год назад +1

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

  • @sayedpritom
    @sayedpritom Год назад

    Helped a lot! Thank you so much.

  • @rorymaguire145
    @rorymaguire145 2 года назад +1

    Great tutorial thank you Colby, helped me out a lot

  • @biplabsharma5344
    @biplabsharma5344 11 месяцев назад

    Subscribed mate amazing content

  • @havefun5519
    @havefun5519 10 месяцев назад

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

    • @colbyfayock
      @colbyfayock  10 месяцев назад

      thanks glad you found it helpful!

  • @lucianalbuquerque
    @lucianalbuquerque 2 года назад

    Perfect video, so clear! THANK YOU !

  • @vighneshs416
    @vighneshs416 Год назад

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

  • @plorandi
    @plorandi Год назад

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

  • @JamesQQuick
    @JamesQQuick 2 года назад +16

    So cool and so easy!

  • @ivan4486
    @ivan4486 4 месяца назад

    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  4 месяца назад

      no problem! glad you eventually found it :)

  • @trevorbryant2006
    @trevorbryant2006 Год назад

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

  • @inspriongaming4992
    @inspriongaming4992 11 месяцев назад

    Thanks bro you explain much better and even saved my time

    • @colbyfayock
      @colbyfayock  11 месяцев назад

      Glad to hear it helped!

  • @rodrigomenezes22
    @rodrigomenezes22 Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад

      no prblem! happy to hear it was helpful

  • @ranatrinchada
    @ranatrinchada Год назад

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

  • @totfosk
    @totfosk Год назад

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

  • @M1szS
    @M1szS Месяц назад +1

    thanks dude, this was rlly helpful

  • @faustozambrano4901
    @faustozambrano4901 Год назад +1

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

  • @umairkhalid8204
    @umairkhalid8204 10 месяцев назад

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

  • @kunaldhuria3935
    @kunaldhuria3935 Год назад

    What an amazing video thank you so much🙏

  • @mohammadalaaelghamry8010
    @mohammadalaaelghamry8010 10 месяцев назад

    Great video. Very useful thank you.

  • @abhishekkumar-kt3uk
    @abhishekkumar-kt3uk Год назад +1

    Amazing, thanks buddy 🙏

  • @user-qs8rs1ul8l
    @user-qs8rs1ul8l 7 месяцев назад

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

    • @colbyfayock
      @colbyfayock  7 месяцев назад

      glad you found it! 🙌

  • @good_hunter7724
    @good_hunter7724 Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад

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

  • @8koi245
    @8koi245 Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад

      😂 similar minds! glad you enjoyed this :)

  • @thelyricsguy3094
    @thelyricsguy3094 Год назад +1

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

  • @alexmir9878
    @alexmir9878 11 месяцев назад

    Great video and article, ty for it 🚀

  • @oswaldoj.avilesibarra3739
    @oswaldoj.avilesibarra3739 2 года назад

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

  • @YanParoni
    @YanParoni 2 года назад +1

    you are amazing colby, thanks so much

  • @happyharsh
    @happyharsh Месяц назад

    Really Helpful. Thanks!

  • @deathdefier45
    @deathdefier45 Год назад

    Top knotch content brother helped a lot at work thanks!

  • @NotBeHaris
    @NotBeHaris 2 года назад +1

    Thanks for sharing great knowledge.

  • @laous
    @laous Год назад

    Thank you very much Colby!

  • @edsonlucas4758
    @edsonlucas4758 8 месяцев назад

    Very good video, helped me a lot!

    • @colbyfayock
      @colbyfayock  8 месяцев назад +1

      thanks! glad to hear that

  • @Fishamble
    @Fishamble Год назад

    Greta teaching style and wonderful presentation. Thanks.

  • @felipespena1124
    @felipespena1124 Год назад

    Great video, super clear! thanks

  • @cvetanstojanovski559
    @cvetanstojanovski559 Год назад

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

  • @eleah2665
    @eleah2665 2 года назад +1

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

  • @thedevmarc5634
    @thedevmarc5634 Год назад

    Wow, this was so easy! Thank you!

  • @reyreyalldayday5708
    @reyreyalldayday5708 2 года назад

    Colby yous the goat. Thanks for the vids dude

  • @jrome_20
    @jrome_20 2 года назад

    Super useful! Thank you so much!

  • @sunithab2704
    @sunithab2704 7 месяцев назад

    Thank you so much for this!!

  • @fabianpetersen2452
    @fabianpetersen2452 Год назад

    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 👍

  • @mdkhorshed2043
    @mdkhorshed2043 9 месяцев назад

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

  • @madsudan9227
    @madsudan9227 Год назад +1

    Thanks for the superb explanation..keep teaching..

  • @atulyakr.2003
    @atulyakr.2003 8 месяцев назад

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

  • @lauraneves9362
    @lauraneves9362 Год назад

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

  • @davediaz25
    @davediaz25 Год назад

    Man, you are a life saver!

  • @appwala3728
    @appwala3728 5 месяцев назад

    Learned too much from this thank you

  • @pouyabh
    @pouyabh 10 месяцев назад +1

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

  • @Madmann16
    @Madmann16 Год назад

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

  • @emirdior
    @emirdior Год назад

    Great explanation, thanks

  • @leonardocitton4627
    @leonardocitton4627 2 года назад +1

    Great content as ever!

  • @nocturno7887
    @nocturno7887 2 года назад

    Thank you man. I managed to implement your solution successfully

    • @colbyfayock
      @colbyfayock  2 года назад

      awesome! glad it worked out 💪

  • @nitinarora4315
    @nitinarora4315 Год назад

    was a life-saver. Thanks a lot.

  • @aSTr0NeRF0999
    @aSTr0NeRF0999 2 года назад +2

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

  • @ajjo5513
    @ajjo5513 2 года назад

    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  2 года назад

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

  • @user-lg9il4ld1p
    @user-lg9il4ld1p 3 месяца назад

    really understandble,thanks bro.

  • @mubashirwaheed474
    @mubashirwaheed474 6 месяцев назад

    Thank you so much Colby

  • @developedbyvarun
    @developedbyvarun Год назад

    Fantastic! You earned your subscription.

    • @colbyfayock
      @colbyfayock  Год назад +1

      thank you!!

    • @developedbyvarun
      @developedbyvarun Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад +1

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

    • @developedbyvarun
      @developedbyvarun Год назад

      Sad!

  • @allanfernz9356
    @allanfernz9356 Год назад

    Great video! You just gained a sub! :)

  • @imanshokryeh5406
    @imanshokryeh5406 Год назад

    thx a lot it was very helpful👍

  • @ebertorresmacedo3667
    @ebertorresmacedo3667 Год назад

    this video helped me alot!

  • @madeit130
    @madeit130 Год назад

    nice tutorial, thank you ;)

  • @CryptoMarketSummary
    @CryptoMarketSummary Год назад

    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  Год назад

      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 Год назад

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

  • @user-ep5yq8ek3e
    @user-ep5yq8ek3e 3 дня назад

    great example, thanks a lot

  • @rohil3023
    @rohil3023 Год назад

    Awesome tutorial!!

  • @patitorodri
    @patitorodri Год назад

    Great, video!!

  • @xikmatillamominov8784
    @xikmatillamominov8784 Год назад

    great video thank you

  • @manoj-k
    @manoj-k 10 месяцев назад

    Learn something whenever I come here❤

    • @colbyfayock
      @colbyfayock  10 месяцев назад

      awesome happy to hear that! 🙌

  • @thiagoleite3255
    @thiagoleite3255 8 месяцев назад

    thx friend, you're a friend.

  • @lucasquadros5600
    @lucasquadros5600 2 года назад

    It helped me a lot, thx

  • @kirakira160
    @kirakira160 2 года назад +1

    Thank you Colby

  • @ademferchichi1408
    @ademferchichi1408 Год назад

    thank you .. you saved me !

  • @bikabas
    @bikabas 2 года назад +1

    Super useful stuff Colby. Thanks a ton. Btw, what's the VS code theme ur using ? Looks awesome ...

    • @colbyfayock
      @colbyfayock  2 года назад +1

      no problem! I'm using Night Owl there marketplace.visualstudio.com/items?itemName=sdras.night-owl

  • @Minu_Shalom
    @Minu_Shalom Год назад

    Köszönjük!

  • @un9286
    @un9286 10 месяцев назад

    subscribed my man

  • @colbyfayock
    @colbyfayock  2 года назад +6

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

    • @somerandomchannel382
      @somerandomchannel382 Год назад

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

    • @colbyfayock
      @colbyfayock  Год назад +1

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

  • @lazyreport
    @lazyreport 10 месяцев назад

    THANK YOU SO MUCHHHH

  • @namanrajput7686
    @namanrajput7686 9 месяцев назад

    very helpful video😌😌😌😌

    • @colbyfayock
      @colbyfayock  9 месяцев назад

      glad to hear that! 🙌

  • @onio907
    @onio907 11 месяцев назад

    great video

  • @emersonvisuals
    @emersonvisuals Год назад +1

    Great video Colby! 😃 - I was just wondering if you knew there was a way to have multiple ref triggers using the react-intersection-observer NPM plugin without duplicating the same code and changing the variable names? I had a look at their documentation but couldn't seem to find much information about it. Thanks once again!

    • @colbyfayock
      @colbyfayock  Год назад +1

      im seeing pretty much the same thing, the only option might be to use the InView component with children if im reading that right

    • @emersonvisuals
      @emersonvisuals Год назад

      @@colbyfayock Thanks for the help Colby! Much appreciated 😄

  • @guy84eilon
    @guy84eilon Год назад

    love it!