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

Поделиться
HTML-код
  • Опубликовано: 21 янв 2025

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

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

    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

  • @JamesQQuick
    @JamesQQuick 3 года назад +17

    So cool and so easy!

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

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

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

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

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

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

  • @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  3 года назад +6

    What's your favorite use case of scroll-based triggers? Reply 👇
    Make sure to subscribe for more! ruclips.net/user/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

  • @lostsunrises7495
    @lostsunrises7495 2 года назад +3

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

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

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

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

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

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

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

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

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

    Köszönjük!

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

      didnt see this until now, thank you!! 🙏

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

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

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

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

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

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

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

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

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

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

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

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

    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

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

    Thanks bro you explain much better and even saved my time

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

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

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

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

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

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

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

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

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

    Great tutorial thank you Colby, helped me out a lot

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

    thanks dude, this was rlly helpful

  • @atulyakr.2003
    @atulyakr.2003 Год назад

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

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

    Thanks for the superb explanation..keep teaching..

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

    Colby yous the goat. Thanks for the vids dude

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

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

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

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

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

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

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

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

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

    you are amazing colby, thanks so much

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

    Greta teaching style and wonderful presentation. Thanks.

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

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

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

    Great content as ever!

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

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

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

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

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

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

  • @aurobindobhuyan2107
    @aurobindobhuyan2107 2 месяца назад

    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? 😭😭😭😭

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

    Subscribed mate amazing content

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

    Learned too much from this thank you

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

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

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

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

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

      no prblem! happy to hear it was helpful

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

    Man, you are a life saver!

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

    Thank you man. I managed to implement your solution successfully

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

      awesome! glad it worked out 💪

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

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

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

    could you explain further what you mean by the paragraph tag takes up too much space? Near 10:05

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

      Hm, it's been a year and trying to remember tbh. It may have to do with the margin / padding. Only thing I can think of

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

    Top knotch content brother helped a lot at work thanks!

  • @RomanHumennyi-z4k
    @RomanHumennyi-z4k Год назад

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

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

    Perfect video, so clear! THANK YOU !

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

    Helped a lot! Thank you so much.

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

    Amazing, thanks buddy 🙏

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

    Great video and article, ty for it 🚀

  • @manoj-k
    @manoj-k Год назад

    Learn something whenever I come here❤

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

      awesome happy to hear that! 🙌

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

    was a life-saver. Thanks a lot.

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

    Great video! You just gained a sub! :)

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

    Very good video, helped me a lot!

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

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

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

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

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

    Thanks for sharing great knowledge.

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

    Wow, this was so easy! Thank you!

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

    What an amazing video thank you so much🙏

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

    Great video. Very useful thank you.

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

    Super useful! Thank you so much!

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

      Framer motion easy to use

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

    I learned something new. Thanks : )

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

    Great explanation, thanks

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

    Thank you so much for this!!

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

    so i did the rocket animation on hover but i don't want it to load again after the animation has already happened ? is there a way to do such thing ?

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

      you can tell the observer to disconenct or unobserve after it triggers. i would think inside of the callback you can fire that, you just need to store the reference of the original observer to trigger that on. im not sure what the difference is between disconnect and unobserve, but they should work: developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/unobserve

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

      @@colbyfayock Thank you for replying, i did the animation purely on css i didn't use the observer (for now) , would that still work somehow ?

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

      @@clay9600 you should be able to specify that you want the animation to only run once instead of forever
      developer.mozilla.org/en-US/docs/Web/CSS/animation
      developer.mozilla.org/en-US/docs/Web/CSS/animation-iteration-count

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

    Hey I am little confused on how to use react-intersection-observer on a list of elements, without doing this "const { ref: ref1, inView: seeRef1} = useInView({triggerOnce: true});" 10 times. I have a container with a list of cards, how can I use react-observer on each card in a optimal way?

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

      hey not totally sure about the package, i know the browser API allows you to pass in a selector which works a bit differently. could try adding it to a parent and parsing the response? not sure if that would be exactly what you need though

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

    Hi Colby! How can I do exactly that animation but when I press the button (rocket emoji in a button)? Thank you so much!

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

      Hey there may be a better way but you could create a state instsnce that will function similarly to the inview value. Update that state on click. Set the class name on click

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

      @@colbyfayock Thank you so much for your reply Colby!

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

    Awesome tutorial!!

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

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

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

    How do we use this if we want to apply the ref to all the element of the list ? the use case is if a particular element user is watching for more the 2 seconds I need to perform some operation.

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

      you could lift the ref to the element above and listen to events that bubble up to that element

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

    Is there any particular reason why when following this tutorial my image is constantly swtiching between a true false value even though i'm sitting stationary above the element in the view port? is it because im running my react application in a dev environment?

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

      I moved the ref to the container element. I guess directly setting the ref on my image element was breaking the functionality. it's working correctly now. Had to rewatch the video one more time to catch the difference. Thanks for the walk through!

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

      @@Buff4eyes glad you worked it out!

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

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

    Thank you so much Colby

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

    It's a brilliant tutorial thanks a lot man, keep going

  • @マサヒロ-m8f
    @マサヒロ-m8f 11 месяцев назад

    really understandble,thanks bro.

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

    this video helped me alot!

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

    Is that what you got to do with framer motion too, or do they have their own stuff for that?

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

      they have www.framer.com/docs/use-in-view/

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

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

    Thank you very much Colby!

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

    I have this problem where If I scroll to where the div is partly visible, the element just keeps flickering. This is probably due the state of isIntersecting switching between false and true at that point of the scroll. Is there a way I can solve this when i've only scrolled to where the div is partly visible?

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

      can you clarify the intent? are you hoping that more of the element needs to be visible before it triggers? or another way to solve, so that it only triggers once and remains visible or whatever the updated state is?

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

      @@colbyfayock to clarify the element is supposed to slide up when scrolled to. The problem is that it flickers when I scroll to a certain point where its just right before the div. I just want the animation to run just once and not flicker when scrolled to that point.

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

      So I think that the area I'm scrolling to is at that breaking point where isIntersecting keeps switching between false and true repeatedly. Because of this, my slide up animation just keeps running and that's what causes the flickering.

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

      @@OneDreamOfWhop you can use `disconnect` on the observer as soon as it triggers to prevent it frmo triggering again developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/disconnect

  • @ММ-ж2з
    @ММ-ж2з 7 месяцев назад

    great example, thanks a lot

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

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