Skip to content

Commit

Permalink
fix-unsafe-lifecycles (#1374)
Browse files Browse the repository at this point in the history
- change componentWillReceiveProps to componentDidUpdate
- update .eslintrc
  • Loading branch information
colinrcummings authored and martijnrusschen committed Jun 22, 2018
1 parent 953c40a commit 5443432
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Expand Up @@ -31,8 +31,8 @@
"react/jsx-uses-vars": 2,
"react/no-danger": 2,
"react/no-deprecated": 2,
"react/no-did-mount-set-state": 2,
"react/no-did-update-set-state": 2,
"react/no-did-mount-set-state": 0,
"react/no-did-update-set-state": 0,
"react/no-direct-mutation-state": 2,
"react/no-multi-comp": 2,
"react/no-set-state": 0,
Expand Down
14 changes: 7 additions & 7 deletions src/calendar.jsx
Expand Up @@ -142,20 +142,20 @@ export default class Calendar extends React.Component {
}
}

componentWillReceiveProps(nextProps) {
componentDidUpdate(prevProps) {
if (
nextProps.preSelection &&
!isSameDay(nextProps.preSelection, this.props.preSelection)
this.props.preSelection &&
!isSameDay(this.props.preSelection, prevProps.preSelection)
) {
this.setState({
date: this.localizeDate(nextProps.preSelection)
date: this.localizeDate(this.props.preSelection)
});
} else if (
nextProps.openToDate &&
!isSameDay(nextProps.openToDate, this.props.openToDate)
this.props.openToDate &&
!isSameDay(this.props.openToDate, prevProps.openToDate)
) {
this.setState({
date: this.localizeDate(nextProps.openToDate)
date: this.localizeDate(this.props.openToDate)
});
}
}
Expand Down
28 changes: 21 additions & 7 deletions src/index.jsx
Expand Up @@ -11,6 +11,7 @@ import {
isDate,
isBefore,
isAfter,
equals,
setTime,
getSecond,
getMinute,
Expand Down Expand Up @@ -52,6 +53,14 @@ function hasPreSelectionChanged(date1, date2) {
return date1 !== date2;
}

function hasSelectionChanged(date1, date2) {
if (date1 && date2) {
return !equals(date1, date2);
}

return false;
}

/**
* General datepicker component.
*/
Expand Down Expand Up @@ -179,19 +188,24 @@ export default class DatePicker extends React.Component {
this.state = this.calcInitialState();
}

componentWillReceiveProps(nextProps) {
componentDidUpdate(prevProps, prevState) {
if (
this.props.inline &&
hasPreSelectionChanged(this.props.selected, nextProps.selected)
prevProps.inline &&
hasPreSelectionChanged(prevProps.selected, this.props.selected)
) {
this.setPreSelection(nextProps.selected);
this.setPreSelection(this.props.selected);
}
if (this.props.highlightDates !== nextProps.highlightDates) {
if (prevProps.highlightDates !== this.props.highlightDates) {
this.setState({
highlightDates: getHightLightDaysMap(nextProps.highlightDates)
highlightDates: getHightLightDaysMap(this.props.highlightDates)
});
}
if (!this.state.focused) this.setState({ inputValue: null });
if (
!prevState.focused &&
hasSelectionChanged(prevProps.selected, this.props.selected)
) {
this.setState({ inputValue: null });
}
}

componentWillUnmount() {
Expand Down

0 comments on commit 5443432

Please sign in to comment.