A Software Engineer and Developer Advocate who loves sharing knowledge via writing, videos, mentorship, and working out. Please Subscribe: https://www.youtube.com/channel/UCcz5Bvr4kGHPFAjvnRhIQ4g
Calling AOS.init(); directly inside of a useEffect might give unexpected issues like: Animation already animated before element comes into view. This is due to useEffect runs when componentMounts and not when the DOMContentLoaded. Had to learn the hard hard way 🥲.
Instead create a useAOS hook like so:
// useAOS.tsximport{useEffect}from'react';import{useWindowSize}from'react-use';constuseAOS=(options:Aos.AosOptions={})=>{constwindowSize=useWindowSize({initialWidth:1024,});useEffect(()=>{letAOS:Aos.Aos|undefined;constinitAOS=async ()=>{AOS=(awaitimport('aos')).default;AOS.init({duration:500,once:true,disable:windowSize?.width>1024,// Disable AOS on small screens...options,});AOS.refreshHard();};initAOS();return ()=>{AOS?.refreshHard();};},[windowSize?.width,options]);// Reinitialize AOS on window size change};exportdefaultuseAOS;// App.tsxconstApp=()=>{useAOS();return(// your code...);}
This allows me to import the AOS library only when the component has mounted, and gave me my expected animation result.
Nice, declarative animation framework can speed things up tremendously.
Of course, one should understand the fundamentals to be able to build their own custom stuff, but for the average user who just wants to spice up their websites/profiles with a little extra on top without spending hours or days practicing CSS, letting someone else handle it makes things a lot easier.
A Software Engineer and Developer Advocate who loves sharing knowledge via writing, videos, mentorship, and working out. Please Subscribe: https://www.youtube.com/channel/UCcz5Bvr4kGHPFAjvnRhIQ4g
A Software Engineer and Developer Advocate who loves sharing knowledge via writing, videos, mentorship, and working out. Please Subscribe: https://www.youtube.com/channel/UCcz5Bvr4kGHPFAjvnRhIQ4g
A Software Engineer and Developer Advocate who loves sharing knowledge via writing, videos, mentorship, and working out. Please Subscribe: https://www.youtube.com/channel/UCcz5Bvr4kGHPFAjvnRhIQ4g
A Software Engineer and Developer Advocate who loves sharing knowledge via writing, videos, mentorship, and working out. Please Subscribe: https://www.youtube.com/channel/UCcz5Bvr4kGHPFAjvnRhIQ4g
I want to add something to the conversation.
If you are using React or NextJS
put your
Inside a useEffect like so
Without useEffect it will generate an error saying "document is not defined"
Thank you very much for your contribution.
Calling AOS.init(); directly inside of a useEffect might give unexpected issues like: Animation already animated before element comes into view. This is due to useEffect runs when componentMounts and not when the DOMContentLoaded. Had to learn the hard hard way 🥲.
Instead create a useAOS hook like so:
This allows me to import the AOS library only when the component has mounted, and gave me my expected animation result.
Nice, declarative animation framework can speed things up tremendously.
Of course, one should understand the fundamentals to be able to build their own custom stuff, but for the average user who just wants to spice up their websites/profiles with a little extra on top without spending hours or days practicing CSS, letting someone else handle it makes things a lot easier.
Yes. Your points are valid.Thank you for taking your time to read through.
Nice Article my bro...it really helped me :)
Thank you.
I am happy you found it helpful
This is Awesome bro!!!
Thank you for taking your time
This is beautiful!
It really is. Thank you for going through 🤗