Created
December 29, 2017 07:43
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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