Skip to content

Commit 239f0a3

Browse files
PzYonjquense
authored andcommitted
fix: use React.createRef instead of string refs (jquense#1282)
1 parent b9fdce4 commit 239f0a3

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

src/Agenda.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import { inRange } from './utils/eventLevels'
1010
import { isSelected } from './utils/selection'
1111

1212
class Agenda extends React.Component {
13+
constructor(props) {
14+
super(props)
15+
this.headerRef = React.createRef()
16+
this.dateColRef = React.createRef()
17+
this.timeColRef = React.createRef()
18+
this.contentRef = React.createRef()
19+
this.tbodyRef = React.createRef()
20+
}
21+
1322
componentDidMount() {
1423
this._adjustHeader()
1524
}
@@ -33,22 +42,22 @@ class Agenda extends React.Component {
3342
<div className="rbc-agenda-view">
3443
{events.length !== 0 ? (
3544
<React.Fragment>
36-
<table ref="header" className="rbc-agenda-table">
45+
<table ref={this.headerRef} className="rbc-agenda-table">
3746
<thead>
3847
<tr>
39-
<th className="rbc-header" ref="dateCol">
48+
<th className="rbc-header" ref={this.dateColRef}>
4049
{messages.date}
4150
</th>
42-
<th className="rbc-header" ref="timeCol">
51+
<th className="rbc-header" ref={this.timeColRef}>
4352
{messages.time}
4453
</th>
4554
<th className="rbc-header">{messages.event}</th>
4655
</tr>
4756
</thead>
4857
</table>
49-
<div className="rbc-agenda-content" ref="content">
58+
<div className="rbc-agenda-content" ref={this.contentRef}>
5059
<table className="rbc-agenda-table">
51-
<tbody ref="tbody">
60+
<tbody ref={this.tbodyRef}>
5261
{range.map((day, idx) => this.renderDay(day, events, idx))}
5362
</tbody>
5463
</table>
@@ -155,15 +164,16 @@ class Agenda extends React.Component {
155164
}
156165

157166
_adjustHeader = () => {
158-
if (!this.refs.tbody) return
167+
if (!this.tbodyRef.current) return
159168

160-
let header = this.refs.header
161-
let firstRow = this.refs.tbody.firstChild
169+
let header = this.headerRef.current
170+
let firstRow = this.tbodyRef.current.firstChild
162171

163172
if (!firstRow) return
164173

165174
let isOverflowing =
166-
this.refs.content.scrollHeight > this.refs.content.clientHeight
175+
this.contentRef.current.scrollHeight >
176+
this.contentRef.current.clientHeight
167177
let widths = this._widths || []
168178

169179
this._widths = [
@@ -172,8 +182,8 @@ class Agenda extends React.Component {
172182
]
173183

174184
if (widths[0] !== this._widths[0] || widths[1] !== this._widths[1]) {
175-
this.refs.dateCol.style.width = this._widths[0] + 'px'
176-
this.refs.timeCol.style.width = this._widths[1] + 'px'
185+
this.dateColRef.current.style.width = this._widths[0] + 'px'
186+
this.timeColRef.current.style.width = this._widths[1] + 'px'
177187
}
178188

179189
if (isOverflowing) {

src/Calendar.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,6 @@ class Calendar extends React.Component {
897897
/>
898898
)}
899899
<View
900-
ref="view"
901900
{...props}
902901
events={events}
903902
date={current}

src/Month.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class MonthView extends React.Component {
2828

2929
this._bgRows = []
3030
this._pendingSelection = []
31+
this.slotRowRef = React.createRef()
3132
this.state = {
3233
rowLimit: 5,
3334
needLimitMeasure: true,
@@ -112,7 +113,7 @@ class MonthView extends React.Component {
112113
return (
113114
<DateContentRow
114115
key={weekIdx}
115-
ref={weekIdx === 0 ? 'slotRow' : undefined}
116+
ref={weekIdx === 0 ? this.slotRowRef : undefined}
116117
container={this.getContainer}
117118
className="rbc-month-row"
118119
getNow={getNow}
@@ -216,7 +217,7 @@ class MonthView extends React.Component {
216217
measureRowLimit() {
217218
this.setState({
218219
needLimitMeasure: false,
219-
rowLimit: this.refs.slotRow.getRowLimit(),
220+
rowLimit: this.slotRowRef.current.getRowLimit(),
220221
})
221222
}
222223

src/Popup.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import EventCell from './EventCell'
99
import { isSelected } from './utils/selection'
1010

1111
class Popup extends React.Component {
12+
constructor(props) {
13+
super(props)
14+
15+
this.rootRef = React.createRef()
16+
}
17+
1218
componentDidMount() {
1319
let { popupOffset = 5 } = this.props,
14-
{ top, left, width, height } = getOffset(this.refs.root),
20+
{ top, left, width, height } = getOffset(this.rootRef.current),
1521
viewBottom = window.innerHeight + getScrollTop(window),
1622
viewRight = window.innerWidth + getScrollLeft(window),
1723
bottom = top + height,
@@ -54,7 +60,7 @@ class Popup extends React.Component {
5460
}
5561

5662
return (
57-
<div ref="root" style={style} className="rbc-overlay">
63+
<div ref={this.rootRef} style={style} className="rbc-overlay">
5864
<div className="rbc-overlay-header">
5965
{localizer.format(slotStart, 'dayHeaderFormat')}
6066
</div>

src/TimeGrid.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class TimeGrid extends Component {
2222
this.state = { gutterWidth: undefined, isOverflowing: null }
2323

2424
this.scrollRef = React.createRef()
25+
this.contentRef = React.createRef()
2526
}
2627

2728
componentWillMount() {
@@ -209,7 +210,7 @@ export default class TimeGrid extends Component {
209210
getDrilldownView={this.props.getDrilldownView}
210211
/>
211212
<div
212-
ref="content"
213+
ref={this.contentRef}
213214
className="rbc-time-content"
214215
onScroll={this.handleScroll}
215216
>
@@ -253,7 +254,7 @@ export default class TimeGrid extends Component {
253254

254255
applyScroll() {
255256
if (this._scrollRatio) {
256-
const { content } = this.refs
257+
const content = this.contentRef.current
257258
content.scrollTop = content.scrollHeight * this._scrollRatio
258259
// Only do this once
259260
this._scrollRatio = null
@@ -272,8 +273,8 @@ export default class TimeGrid extends Component {
272273
checkOverflow = () => {
273274
if (this._updatingOverflow) return
274275

275-
let isOverflowing =
276-
this.refs.content.scrollHeight > this.refs.content.clientHeight
276+
const content = this.contentRef.current
277+
let isOverflowing = content.scrollHeight > content.clientHeight
277278

278279
if (this.state.isOverflowing !== isOverflowing) {
279280
this._updatingOverflow = true

0 commit comments

Comments
 (0)