-
Notifications
You must be signed in to change notification settings - Fork 308
Animate on change #474
Comments
There isn't a super nice way to do this. I think you'll have to remove the animation when it completes and then add it again, see this example jsbin. You might be better off using something like |
Thanks for the quick response. I took a similar approach using vanilla css: render() {
const points = this.props.points
let className = 'Score'
// animation class
if (this.state.points != points) {
className += ' Score--enter'
setTimeout(() => this.setState({points: points}), 3020)
}
return <div className={className}>
Score: {points}
</div>
} Edit: Fixed buggy |
So, /**
* Animated component. Adds `animationClassName` when `animate` is true,
* then removes `animationClassName` when animation is done (event
* `animationend` is triggered).
*
* @prop {string} className - Base class name.
* @prop {string} animationClassName - Class added when `animate == true`.
* @prop {bool} animate - Wheter to animate component.
*/
class Animated extends Component {
constructor(props) {
super(props)
this.state = { animating: false, clearAnimationClass: false }
}
componentDidMount() {
this.refs.root.addEventListener('animationstart', () => {
this.setState({ animating: true, clearAnimationClass: false })
})
this.refs.root.addEventListener('animationend', () => {
// send separate, animation state change will not render
this.setState({ clearAnimationClass: true }) // renders
this.setState({ animating: false, clearAnimationClass: false })
})
}
shouldComponentUpdate(nextProps, nextState) {
if (this.state.animating != nextState.animating){
// do not render on animation change
return false
}
return true
}
render() {
let className = this.props.className
if (this.props.animate && !this.state.clearAnimationClass) {
className += ` ${this.props.animationClassName}`
}
return <span ref="root" className={className}>
{this.props.children}
</span>
}
} Usage:
This could of course be useful in radium also. |
I don't think |
If I understand it correctly, Regarding support, according to can i use and the spec, the event should be available if CSS animation is supported. Global support is 90% as of now, but I have no experience if this actually is the case. |
Sorry for the lack of context, I was referring to this quote:
In this issue: facebook/react#1326 Way to go publishing a package for this, though! |
I'm into the micro-modules-thingy |
Hi!
Is it possible to animate on state change?
I use this code
which only animates on first enter.
The text was updated successfully, but these errors were encountered: