Skip to content

Instantly share code, notes, and snippets.

@zhusee2
Created December 29, 2017 07:43
Show Gist options
  • Save zhusee2/32f42d9fbc2c7cfa1e5d1aad519d98a8 to your computer and use it in GitHub Desktop.
Save zhusee2/32f42d9fbc2c7cfa1e5d1aad519d98a8 to your computer and use it in GitHub Desktop.
Concept for a React Component displaying an CSS Animation to indicate its contents has been updated.
import React from 'react';
import PropTypes from 'prop-types';
const ANIMATION_DURATION = 750; // for example
class FooBar extends React.Component {
static propTypes = {
lastUpdated: PropTypes.string,
};
static defaultProps = {
lastUpdated: Date.now(),
};
state = {
shouldAnimate: false,
};
componentWillReceiveProps(nextProps) {
if (nextProps.lastUpdated !== this.props.lastUpdate) {
this.animate();
}
}
animate() {
this.setState({ shouldAnimate: true });
setTimeout(() => this.setState({ shouldAnimate: false }), ANIMATION_DURATION);
}
render() {
const className = this.state.shouldAnimate ? 'animate': null;
return <div className={className}>Foo Bar</div>;
}
}
export default FooBar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment