Skip to content

Instantly share code, notes, and snippets.

@charliewilco
Last active November 8, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charliewilco/345dc6021be8220de7b8ee0578ae76be to your computer and use it in GitHub Desktop.
Save charliewilco/345dc6021be8220de7b8ee0578ae76be to your computer and use it in GitHub Desktop.
Ball.js
import React from 'react'
export default class extends React.Component {
state = {
positionY: 0
}
static displayName = 'Ball'
moveDown = () => this.setState(({ positionY }) => ({ positionY: positionY + 2 }))
componentDidMount() {
this.interval = setInterval(() => this.moveDown(), 500);
}
componentWillUnmount() {
clearInterval(this.interval);
}
componentDidUpdate(nextProps, nextState) {
const position = this.ball.getBoundingClientRect()
if (position.y === this.props.containerHeight) {
this.props.decrement()
clearInterval(this.interval)
}
}
render () {
const { onClick } = this.props
return (
<div ref={x => this.ball = x} onClick={onClick} style={{ transform: translateY(${this.state.positionY}px) }} className='Ball' />
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment