AOS with Dynamic Content in React

TL;DR  Using AOS, ReactWebpack & Babel

import '../node_modules/aos/dist/aos.css';
import React from 'react';
import AOS from 'aos';

class HomePage extends React.Component {
  constructor(props, context) {
    super(props, context);
    AOS.init();
  }

  componentWillReceiveProps (){
    AOS.refresh();
  }

  render(){
    return (
<div data-aos="fade-up">{this.props.dynamicContent}</div>
);
  }
}

Quick Explanation

When you load content asynchronously using, callbacks, promises or something like the redux-thunk library, it can sometimes change the height of your DOM after the inital mount of the component where you call AOS.init(). This will throw off the calculations in AOS (Animations On-Scroll). AOS has a built-in method to fix this AOS.refresh() but the question is, when to use it. React lifecycle methods to the rescue. Simple call AOS.refresh() every time the component receives new props using the componentWillReceiveProps lifecycle method. That way, when you receive the asynchronously loaded content, it will refresh AOS and all your animations will trigger at the correct time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s