DEV Community

CSS Animation Without CSS - AOS in Plain JS and React

NJOKU SAMSON EBERE on July 06, 2020

"CSS is beautiful but hard". Most developers can agree on that statement. In fact many backend developers left the frontend because of the diffi...
Collapse
 
shareef profile image
Mohammed Nadeem Shareef

I want to add something to the conversation.
If you are using React or NextJS

put your

AOS.init()
Enter fullscreen mode Exit fullscreen mode

Inside a useEffect like so

useEffect(() => {
    AOS.init();
},[])
Enter fullscreen mode Exit fullscreen mode

Without useEffect it will generate an error saying "document is not defined"

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you very much for your contribution.

Collapse
 
chibykes profile image
Chibykes • Edited

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.tsx
import { useEffect } from 'react';
import { useWindowSize } from 'react-use';

const useAOS = (options: Aos.AosOptions = {}) => {
  const windowSize = useWindowSize({
    initialWidth: 1024,
  });

  useEffect(() => {
    let AOS: Aos.Aos | undefined;
    const initAOS = async () => {
      AOS = (await import('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

};

export default useAOS;




// App.tsx
const App = () => {
   useAOS();

  return(
    // your code...
  );
}
Enter fullscreen mode Exit fullscreen mode

This allows me to import the AOS library only when the component has mounted, and gave me my expected animation result.

Collapse
 
mxldevs profile image
MxL Devs • Edited

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.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Yes. Your points are valid.Thank you for taking your time to read through.

Collapse
 
udokaokoye profile image
Udoka Okoye

Nice Article my bro...it really helped me :)

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you.
I am happy you found it helpful

Collapse
 
amankumar001 profile image
Aman Kumar

This is Awesome bro!!!

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you for taking your time

Collapse
 
reworkingwork profile image
Reworking

This is beautiful!

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

It really is. Thank you for going through 🤗