Skip to content

Commit

Permalink
Use componentDidUpdate lifecycle instead of derived state
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Raudabaugh committed Dec 23, 2019
1 parent 9f1bebc commit 0ff679c
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,8 @@ import PropTypes from 'prop-types'

class ChartistGraph extends Component {

constructor(props) {
super(props)
this.state = {
type: '',
}
}

displayName: 'ChartistGraph'

static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.type !== prevState.type) {
return { ...nextProps }
}
return null;
}

componentWillUnmount() {
if (this.chartist) {
try {
Expand All @@ -33,6 +19,10 @@ class ChartistGraph extends Component {
this.updateChart(this.props);
}

componentDidUpdate() {
this.updateChart(this.props);
}

updateChart(config) {
let Chartist = require('chartist');

Expand Down Expand Up @@ -67,8 +57,8 @@ class ChartistGraph extends Component {
})
));
return (
<div className={`ct-chart ${className || ''}`} ref={(ref) => this.chart = ref } style={style}>
{childrenWithProps}
<div className={`ct-chart ${className || ''}`} ref={(ref) => this.chart = ref} style={style}>
{childrenWithProps}
</div>
)
}
Expand Down

0 comments on commit 0ff679c

Please sign in to comment.