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

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ก.พ. 2025

ความคิดเห็น • 344

  • @colbyfayock
    @colbyfayock  6 หลายเดือนก่อน

    Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course

  • @loop8836
    @loop8836 2 ปีที่แล้ว +8

    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  2 ปีที่แล้ว +1

      Really glad to hear it helped!

  • @hamzaeshoul
    @hamzaeshoul ปีที่แล้ว +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  ปีที่แล้ว +3

      thanks for the kind words!! glad it was helpful

  • @John-eq5cd
    @John-eq5cd ปีที่แล้ว +2

    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  ปีที่แล้ว

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

  • @JatinS-yt
    @JatinS-yt ปีที่แล้ว +1

    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!

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      glad to hear it helped!! 🙌

  • @KingPacavision
    @KingPacavision ปีที่แล้ว +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  ปีที่แล้ว

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

  • @JamesQQuick
    @JamesQQuick 3 ปีที่แล้ว +17

    So cool and so easy!

    • @colbyfayock
      @colbyfayock  3 ปีที่แล้ว +1

      thanks James! 🙌

  • @colbyfayock
    @colbyfayock  3 ปีที่แล้ว +6

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

    • @somerandomchannel382
      @somerandomchannel382 2 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว +1

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

  • @ashimxtha6407
    @ashimxtha6407 ปีที่แล้ว +1

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

  • @Minu_Shalom
    @Minu_Shalom 2 ปีที่แล้ว

    Köszönjük!

    • @colbyfayock
      @colbyfayock  7 หลายเดือนก่อน

      didnt see this until now, thank you!! 🙏

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

  • @simple_user000
    @simple_user000 ปีที่แล้ว

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

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

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว +1

      Thanks glad to hear that!

  • @cvetanstojanovski559
    @cvetanstojanovski559 2 ปีที่แล้ว

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

  • @havefun5519
    @havefun5519 ปีที่แล้ว

    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  ปีที่แล้ว

      thanks glad you found it helpful!

  • @marktheunknown1829
    @marktheunknown1829 9 หลายเดือนก่อน

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

    • @colbyfayock
      @colbyfayock  9 หลายเดือนก่อน

      No problem!

  • @ivan4486
    @ivan4486 ปีที่แล้ว

    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  ปีที่แล้ว

      no problem! glad you eventually found it :)

  • @eleah2665
    @eleah2665 3 ปีที่แล้ว +1

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

    • @colbyfayock
      @colbyfayock  3 ปีที่แล้ว

      definitely a useful one :)

  • @8koi245
    @8koi245 2 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      😂 similar minds! glad you enjoyed this :)

  • @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.

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      happy to hear this helped!

  • @lostsunrises7495
    @lostsunrises7495 2 ปีที่แล้ว +3

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      really glad to hear that!

  • @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!

  • @CryptoMarketSummary
    @CryptoMarketSummary 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      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 2 ปีที่แล้ว

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

  • @totfosk
    @totfosk ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      Thanks! Glad it helped

  • @Madmann16
    @Madmann16 2 ปีที่แล้ว

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

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

  • @faustozambrano4901
    @faustozambrano4901 2 ปีที่แล้ว +1

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      Wiggle wiggle wiggle

  • @atulyakr.2003
    @atulyakr.2003 ปีที่แล้ว

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

  • @fabianpetersen2452
    @fabianpetersen2452 2 ปีที่แล้ว

    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 👍

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      thanks, glad to hear that!

  • @trevorbryant2006
    @trevorbryant2006 2 ปีที่แล้ว

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

  • @vighneshs416
    @vighneshs416 2 ปีที่แล้ว

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

  • @athmanbakari9724
    @athmanbakari9724 2 ปีที่แล้ว +1

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      thank you! glad to hear that :D

  • @anthonyiu
    @anthonyiu ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      Awesome! Thank you 😁

  • @pouyabh
    @pouyabh ปีที่แล้ว +1

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

  • @inspriongaming4992
    @inspriongaming4992 ปีที่แล้ว

    Thanks bro you explain much better and even saved my time

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      Glad to hear it helped!

  • @ajjo5513
    @ajjo5513 3 ปีที่แล้ว

    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  3 ปีที่แล้ว

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

  • @mordicai4296
    @mordicai4296 2 ปีที่แล้ว +1

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

  • @manoj-k
    @manoj-k ปีที่แล้ว

    Learn something whenever I come here❤

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      awesome happy to hear that! 🙌

  • @w2ytube
    @w2ytube 3 ปีที่แล้ว +1

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

  • @M1szS
    @M1szS 9 หลายเดือนก่อน +1

    thanks dude, this was rlly helpful

    • @colbyfayock
      @colbyfayock  9 หลายเดือนก่อน +1

      no problem!

  • @CaliburPANDAs
    @CaliburPANDAs 2 ปีที่แล้ว +3

    What about libraries with built-in scroll features that uses the intersection observer APi such as React-Spring, Framer-motion, AOS? 👀

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      hey not quite sure, what's the question?

    • @artem_egamediev
      @artem_egamediev 9 หลายเดือนก่อน

      Framer motion easy to use

  • @AjvarRelish
    @AjvarRelish ปีที่แล้ว

    Thank you for this! It's exactly what I've been looking for! One question on the css, why do you type styles. Before tha actual class you're using? Im a front end student and have not seen that before and would love to understand it

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      hey can you let me know which part you're referring to?

  • @madsudan9227
    @madsudan9227 2 ปีที่แล้ว +1

    Thanks for the superb explanation..keep teaching..

  • @Fishamble
    @Fishamble 2 ปีที่แล้ว

    Greta teaching style and wonderful presentation. Thanks.

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      thanks and your welcome!

  • @appwala3728
    @appwala3728 ปีที่แล้ว

    Learned too much from this thank you

  • @thelyricsguy3094
    @thelyricsguy3094 2 ปีที่แล้ว +1

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

  • @umairkhalid8204
    @umairkhalid8204 ปีที่แล้ว

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

  • @rodrigomenezes22
    @rodrigomenezes22 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      no prblem! happy to hear it was helpful

  • @YanParoni
    @YanParoni 2 ปีที่แล้ว +1

    you are amazing colby, thanks so much

  • @davediaz25
    @davediaz25 2 ปีที่แล้ว

    Man, you are a life saver!

  • @reyreyalldayday5708
    @reyreyalldayday5708 3 ปีที่แล้ว

    Colby yous the goat. Thanks for the vids dude

  • @ranatrinchada
    @ranatrinchada ปีที่แล้ว

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

  • @rorymaguire145
    @rorymaguire145 3 ปีที่แล้ว +1

    Great tutorial thank you Colby, helped me out a lot

  • @mdkhorshed2043
    @mdkhorshed2043 ปีที่แล้ว

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

  • @deathdefier45
    @deathdefier45 2 ปีที่แล้ว

    Top knotch content brother helped a lot at work thanks!

  • @juanortegaa6916
    @juanortegaa6916 3 ปีที่แล้ว

    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  3 ปีที่แล้ว

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

  • @abhishekkumar-kt3uk
    @abhishekkumar-kt3uk ปีที่แล้ว +1

    Amazing, thanks buddy 🙏

  • @growagencymx
    @growagencymx 2 ปีที่แล้ว

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

  • @aSTr0NeRF0999
    @aSTr0NeRF0999 2 ปีที่แล้ว +2

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

  • @biplabsharma5344
    @biplabsharma5344 ปีที่แล้ว

    Subscribed mate amazing content

  • @RomanHumennyi-z4k
    @RomanHumennyi-z4k ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      glad you found it! 🙌

  • @nocturno7887
    @nocturno7887 2 ปีที่แล้ว

    Thank you man. I managed to implement your solution successfully

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      awesome! glad it worked out 💪

  • @alexmir9878
    @alexmir9878 ปีที่แล้ว

    Great video and article, ty for it 🚀

  • @RizaHariati
    @RizaHariati 2 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      No problem 🙌 glad to hear that

  • @sayedpritom
    @sayedpritom 2 ปีที่แล้ว

    Helped a lot! Thank you so much.

  • @bikabas
    @bikabas 3 ปีที่แล้ว +1

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

    • @colbyfayock
      @colbyfayock  3 ปีที่แล้ว +1

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

  • @bancroftberlin
    @bancroftberlin 2 ปีที่แล้ว

    I'm just getting into UI animations, thanks for the tutorial. I noticed that the current Apple product pages are staying away from animations. Maybe that got bad feedback, too annoying? Dosing them the right way is part of my learning UI animations.

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      no problem! definitely don't want to overdo them, but the right amount can be delightful

  • @edsonlucas4758
    @edsonlucas4758 ปีที่แล้ว

    Very good video, helped me a lot!

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว +1

      thanks! glad to hear that

  • @lauraneves9362
    @lauraneves9362 2 ปีที่แล้ว

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

  • @plorandi
    @plorandi 2 ปีที่แล้ว

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

  • @leonardocitton4627
    @leonardocitton4627 3 ปีที่แล้ว +1

    Great content as ever!

  • @johny962
    @johny962 6 หลายเดือนก่อน

    I learned something new. Thanks : )

    • @colbyfayock
      @colbyfayock  6 หลายเดือนก่อน

      no problem!

  • @w3mw
    @w3mw ปีที่แล้ว

    So cool, the way apple had in the beggining of the video, where all items smoothly goes away with opacity how is that possible in scroll?

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว

      There are different ways to handle this but it's definitely possible they use this technique! I like using the css animation but you could do it all in JS for instance

  • @albinsjolin649
    @albinsjolin649 2 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว

      glad to hear that! 🙌

  • @developedbyvarun
    @developedbyvarun 2 ปีที่แล้ว

    Fantastic! You earned your subscription.

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว +1

      thank you!!

    • @developedbyvarun
      @developedbyvarun 2 ปีที่แล้ว

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

    • @colbyfayock
      @colbyfayock  2 ปีที่แล้ว +1

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

    • @developedbyvarun
      @developedbyvarun 2 ปีที่แล้ว

      Sad!

  • @ankanbasu7381
    @ankanbasu7381 ปีที่แล้ว

    thanks for the tutorial.
    and shouldn't we use useEffect cleanup to remove the intersection observer?

    • @colbyfayock
      @colbyfayock  ปีที่แล้ว +1

      yes! absolutely. great point!

  • @kunaldhuria3935
    @kunaldhuria3935 ปีที่แล้ว

    What an amazing video thank you so much🙏

  • @allanfernz9356
    @allanfernz9356 ปีที่แล้ว

    Great video! You just gained a sub! :)

  • @mohammadalaaelghamry8010
    @mohammadalaaelghamry8010 ปีที่แล้ว

    Great video. Very useful thank you.

  • @nitinarora4315
    @nitinarora4315 2 ปีที่แล้ว

    was a life-saver. Thanks a lot.

  • @emirdior
    @emirdior ปีที่แล้ว

    Great explanation, thanks

  • @NotBeHaris
    @NotBeHaris 3 ปีที่แล้ว +1

    Thanks for sharing great knowledge.

  • @sunithab2704
    @sunithab2704 ปีที่แล้ว

    Thank you so much for this!!

  • @ММ-ж2з
    @ММ-ж2з 8 หลายเดือนก่อน

    great example, thanks a lot

    • @colbyfayock
      @colbyfayock  8 หลายเดือนก่อน

      no problem!

  • @lucianalbuquerque
    @lucianalbuquerque 2 ปีที่แล้ว

    Perfect video, so clear! THANK YOU !

  • @vmrsilva
    @vmrsilva 28 วันที่ผ่านมา

    Amazing! 🚀🚀🚀

    • @colbyfayock
      @colbyfayock  25 วันที่ผ่านมา

      thank you 🙏

  • @laous
    @laous 2 ปีที่แล้ว

    Thank you very much Colby!

  • @thedevmarc5634
    @thedevmarc5634 2 ปีที่แล้ว

    Wow, this was so easy! Thank you!

  • @mubashirwaheed474
    @mubashirwaheed474 ปีที่แล้ว

    Thank you so much Colby

  • @マサヒロ-m8f
    @マサヒロ-m8f 11 หลายเดือนก่อน

    really understandble,thanks bro.

    • @colbyfayock
      @colbyfayock  11 หลายเดือนก่อน

      no problem!

  • @emersonvisuals
    @emersonvisuals 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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 2 ปีที่แล้ว

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

  • @jrome_20
    @jrome_20 2 ปีที่แล้ว

    Super useful! Thank you so much!

  • @aurobindobhuyan2107
    @aurobindobhuyan2107 3 หลายเดือนก่อน

    I am using Next.js 14.2, I want to use the scroll-based animation, the animation-timeline works but it's experimental and the safari browser doesn't support it. And making use of hooks will make my entire section client-component. Which also I don't want,
    Can someone please help me with this? 😭😭😭😭

  • @rohil3023
    @rohil3023 2 ปีที่แล้ว

    Awesome tutorial!!

  • @kirakira160
    @kirakira160 3 ปีที่แล้ว +1

    Thank you Colby

    • @colbyfayock
      @colbyfayock  3 ปีที่แล้ว

      you're quite welcome! :)

  • @ebertorresmacedo3667
    @ebertorresmacedo3667 2 ปีที่แล้ว

    this video helped me alot!

  • @thiagoleite3255
    @thiagoleite3255 ปีที่แล้ว

    thx friend, you're a friend.

  • @imanshokryeh5406
    @imanshokryeh5406 2 ปีที่แล้ว

    thx a lot it was very helpful👍

  • @patitorodri
    @patitorodri ปีที่แล้ว

    Great, video!!

  • @sujoydutta920
    @sujoydutta920 3 ปีที่แล้ว

    YOu are right the possibilities are endless 😲