Skip to content

Commit

Permalink
feat: replace unsafe deprecated methods (#2216)
Browse files Browse the repository at this point in the history
This replaces deprecated React methods from the components, leading the way to eventual StrictMode compliance.

#1200 #1777 #1481 #2126 #2104 #2105 #1526
  • Loading branch information
cutterbl committed Jul 8, 2022
1 parent 1b7d8bf commit c5c6a8b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/BackgroundCells.js
Expand Up @@ -24,10 +24,10 @@ class BackgroundCells extends React.Component {
this._teardownSelectable()
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.selectable && !this.props.selectable) this._selectable()
componentDidUpdate(prevProps) {
if (!prevProps.selectable && this.props.selectable) this._selectable()

if (!nextProps.selectable && this.props.selectable)
if (prevProps.selectable && !this.props.selectable)
this._teardownSelectable()
}

Expand Down
8 changes: 4 additions & 4 deletions src/Calendar.js
Expand Up @@ -877,14 +877,14 @@ class Calendar extends React.Component {
super(...args)

this.state = {
context: this.getContext(this.props),
context: Calendar.getContext(this.props),
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({ context: this.getContext(nextProps) })
static getDerivedStateFromProps(nextProps) {
return { context: Calendar.getContext(nextProps) }
}

getContext({
static getContext({
startAccessor,
endAccessor,
allDayAccessor,
Expand Down
7 changes: 3 additions & 4 deletions src/DateContentRow.js
Expand Up @@ -48,10 +48,9 @@ class DateContentRow extends React.Component {
getRowLimit() {
/* Guessing this only gets called on the dummyRow */
const eventHeight = getHeight(this.eventRowRef.current)
const headingHeight =
this.headingRowRef && this.headingRowRef.current
? getHeight(this.headingRowRef.current)
: 0
const headingHeight = this.headingRowRef?.current
? getHeight(this.headingRowRef.current)
: 0
const eventSpace = getHeight(this.containerRef.current) - headingHeight

return Math.max(Math.floor(eventSpace / eventHeight), 1)
Expand Down
10 changes: 4 additions & 6 deletions src/DayColumn.js
Expand Up @@ -38,15 +38,13 @@ class DayColumn extends React.Component {
this.clearTimeIndicatorInterval()
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.selectable && !this.props.selectable) this._selectable()
if (!nextProps.selectable && this.props.selectable)
componentDidUpdate(prevProps, prevState) {
if (!prevProps.selectable && this.props.selectable) this._selectable()
if (prevProps.selectable && !this.props.selectable)
this._teardownSelectable()

this.slotMetrics = this.slotMetrics.update(nextProps)
}
this.slotMetrics = this.slotMetrics.update(this.props)

componentDidUpdate(prevProps, prevState) {
const { getNow, isNow, localizer, date, min, max } = this.props
const getNowChanged = localizer.neq(prevProps.getNow(), getNow(), 'minutes')

Expand Down
11 changes: 6 additions & 5 deletions src/Month.js
Expand Up @@ -27,6 +27,7 @@ class MonthView extends React.Component {
this.state = {
rowLimit: 5,
needLimitMeasure: true,
date: null,
}
this.containerRef = createRef()
this.slotRowRef = createRef()
Expand All @@ -35,11 +36,11 @@ class MonthView extends React.Component {
this._pendingSelection = []
}

UNSAFE_componentWillReceiveProps({ date }) {
const { date: propsDate, localizer } = this.props
this.setState({
needLimitMeasure: localizer.neq(date, propsDate, 'month'),
})
static getDerivedStateFromProps({ date, localizer }, state) {
return {
date,
needLimitMeasure: localizer.neq(date, state.date, 'month'),
}
}

componentDidMount() {
Expand Down
24 changes: 4 additions & 20 deletions src/TimeGrid.js
Expand Up @@ -26,17 +26,17 @@ export default class TimeGrid extends Component {
this.gutterRef = createRef()
}

UNSAFE_componentWillMount() {
getSnapshotBeforeUpdate() {
this.calculateScroll()
}

componentDidMount() {
this.checkOverflow()

if (this.props.width == null) {
this.measureGutter()
}
return null
}

componentDidMount() {
this.applyScroll()

window.addEventListener('resize', this.handleResize)
Expand Down Expand Up @@ -64,23 +64,7 @@ export default class TimeGrid extends Component {
}

componentDidUpdate() {
if (this.props.width == null) {
this.measureGutter()
}

this.applyScroll()
//this.checkOverflow()
}

UNSAFE_componentWillReceiveProps(nextProps) {
const { range, scrollToTime, localizer } = this.props
// When paginating, reset scroll
if (
localizer.neq(nextProps.range[0], range[0], 'minutes') ||
localizer.neq(nextProps.scrollToTime, scrollToTime, 'minutes')
) {
this.calculateScroll(nextProps)
}
}

handleSelectAlldayEvent = (...args) => {
Expand Down
2 changes: 1 addition & 1 deletion stories/props/scrollToTime.stories.js
Expand Up @@ -36,5 +36,5 @@ ScrollToTime.args = {
defaultView: Views.WEEK,
events: demoEvents,
localizer: mLocalizer,
scrollToTime: new Date(1972, 0, 1, 10),
scrollToTime: new Date(1972, 0, 1, 22),
}

0 comments on commit c5c6a8b

Please sign in to comment.