Skip to content

Commit dcc2395

Browse files
colinrcummingsmartijnrusschen
authored andcommitted
fix-unsafe-lifecycles (Hacker0x01#1374)
- change componentWillReceiveProps to componentDidUpdate - update .eslintrc
1 parent bcd5457 commit dcc2395

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"react/jsx-uses-vars": 2,
3232
"react/no-danger": 2,
3333
"react/no-deprecated": 2,
34-
"react/no-did-mount-set-state": 2,
35-
"react/no-did-update-set-state": 2,
34+
"react/no-did-mount-set-state": 0,
35+
"react/no-did-update-set-state": 0,
3636
"react/no-direct-mutation-state": 2,
3737
"react/no-multi-comp": 2,
3838
"react/no-set-state": 0,

src/calendar.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ export default class Calendar extends React.Component {
142142
}
143143
}
144144

145-
componentWillReceiveProps(nextProps) {
145+
componentDidUpdate(prevProps) {
146146
if (
147-
nextProps.preSelection &&
148-
!isSameDay(nextProps.preSelection, this.props.preSelection)
147+
this.props.preSelection &&
148+
!isSameDay(this.props.preSelection, prevProps.preSelection)
149149
) {
150150
this.setState({
151-
date: this.localizeDate(nextProps.preSelection)
151+
date: this.localizeDate(this.props.preSelection)
152152
});
153153
} else if (
154-
nextProps.openToDate &&
155-
!isSameDay(nextProps.openToDate, this.props.openToDate)
154+
this.props.openToDate &&
155+
!isSameDay(this.props.openToDate, prevProps.openToDate)
156156
) {
157157
this.setState({
158-
date: this.localizeDate(nextProps.openToDate)
158+
date: this.localizeDate(this.props.openToDate)
159159
});
160160
}
161161
}

src/index.jsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
isDate,
1212
isBefore,
1313
isAfter,
14+
equals,
1415
setTime,
1516
getSecond,
1617
getMinute,
@@ -52,6 +53,14 @@ function hasPreSelectionChanged(date1, date2) {
5253
return date1 !== date2;
5354
}
5455

56+
function hasSelectionChanged(date1, date2) {
57+
if (date1 && date2) {
58+
return !equals(date1, date2);
59+
}
60+
61+
return false;
62+
}
63+
5564
/**
5665
* General datepicker component.
5766
*/
@@ -179,19 +188,24 @@ export default class DatePicker extends React.Component {
179188
this.state = this.calcInitialState();
180189
}
181190

182-
componentWillReceiveProps(nextProps) {
191+
componentDidUpdate(prevProps, prevState) {
183192
if (
184-
this.props.inline &&
185-
hasPreSelectionChanged(this.props.selected, nextProps.selected)
193+
prevProps.inline &&
194+
hasPreSelectionChanged(prevProps.selected, this.props.selected)
186195
) {
187-
this.setPreSelection(nextProps.selected);
196+
this.setPreSelection(this.props.selected);
188197
}
189-
if (this.props.highlightDates !== nextProps.highlightDates) {
198+
if (prevProps.highlightDates !== this.props.highlightDates) {
190199
this.setState({
191-
highlightDates: getHightLightDaysMap(nextProps.highlightDates)
200+
highlightDates: getHightLightDaysMap(this.props.highlightDates)
192201
});
193202
}
194-
if (!this.state.focused) this.setState({ inputValue: null });
203+
if (
204+
!prevState.focused &&
205+
hasSelectionChanged(prevProps.selected, this.props.selected)
206+
) {
207+
this.setState({ inputValue: null });
208+
}
195209
}
196210

197211
componentWillUnmount() {

0 commit comments

Comments
 (0)