diff --git a/src/Rating.js b/src/Rating.js index c0d8ac6..3063e78 100644 --- a/src/Rating.js +++ b/src/Rating.js @@ -17,11 +17,11 @@ class Rating extends React.PureComponent { this.symbolClick = this.symbolClick.bind(this); } - UNSAFE_componentWillReceiveProps(nextProps) { - const valueChanged = this.props.value !== nextProps.value; - this.setState((prevState) => ({ - displayValue: valueChanged ? nextProps.value : prevState.displayValue - })); + static getDerivedStateFromProps(props, prevState) { + const { value } = props; + return (value === prevState.displayValue) + ? { displayValue: value } + : null; } componentDidUpdate(prevProps, prevState) { diff --git a/src/RatingAPILayer.js b/src/RatingAPILayer.js index c6a0443..eda2662 100644 --- a/src/RatingAPILayer.js +++ b/src/RatingAPILayer.js @@ -14,10 +14,11 @@ class RatingAPILayer extends React.PureComponent { this.handleHover = this.handleHover.bind(this); } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState({ - value: nextProps.initialRating - }); + static getDerivedStateFromProps(props, prevState) { + const { initialRating } = props; + return (initialRating !== prevState.value) + ? { value: initialRating } + : null; } handleClick(value, e) { diff --git a/test/Rating-test.js b/test/Rating-test.js index e131999..a1d808f 100644 --- a/test/Rating-test.js +++ b/test/Rating-test.js @@ -60,20 +60,20 @@ describe('Rating', function () { }); it('should have all symbols readonly', function () { - rating.props.children.forEach(function (symbol, i) { + rating.props.children.forEach(function (symbol) { expect(symbol.props.readonly).to.be.false; }); }); it('should not have mouse move handler', function () { var noop = require('../src/utils/noop'); - rating.props.children.forEach(function (symbol, i) { + rating.props.children.forEach(function (symbol) { expect(symbol.props.onMouseMove).to.equal(noop); }); }); it('should not have click handler', function () { var noop = require('../src/utils/noop'); - rating.props.children.forEach(function (symbol, i) { + rating.props.children.forEach(function (symbol) { expect(symbol.props.onClick).to.equal(noop); }); });