Skip to content

Commit

Permalink
Merge pull request #65 from woofers/update-options
Browse files Browse the repository at this point in the history
Update options when new props are received
  • Loading branch information
woofers authored Jan 3, 2020
2 parents 17aa47e + 2912749 commit 09c924e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-wavify-demo",
"homepage": "http://woofers.github.io/react-wavify",
"version": "1.2.1",
"version": "1.2.2",
"dependencies": {
"@emotion/core": "^10.0.22",
"@fortawesome/fontawesome-svg-core": "^1.2.25",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-wavify",
"version": "1.2.1",
"version": "1.2.2",
"description": "Animated wave component for React",
"main": "dist/react-wavify.min.js",
"src": "src/wave.js",
Expand Down
21 changes: 19 additions & 2 deletions src/wave.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class Wave extends Component {
super(props)
this.container = React.createRef()
this.state = { path: '' }
this.options = {
this.defaults = {
height: 20,
amplitude: 20,
speed: 0.15,
points: 3,
...props.options
}
this.options = { ...props.options, ...this.defaults }
this.lastUpdate = 0
this.elapsed = 0
this.step = 0
Expand Down Expand Up @@ -84,6 +84,23 @@ class Wave extends Component {
this.lastUpdate = new Date()
}

componentDidUpdate(prevProps) {
const transfer = key => {
if (this.options[key] !== this.props.options[key]) {
if (typeof this.props.options[key] === 'undefined') {
this.options[key] = this.defaults[key]
}
else {
this.options[key] = this.props.options[key]
}
}
}
transfer('height')
transfer('amplitude')
transfer('speed')
transfer('points')
}

componentDidMount () {
if (!this.frameId) {
this.resume()
Expand Down

0 comments on commit 09c924e

Please sign in to comment.