Skip to content

Commit 7d59da7

Browse files
committed
Minor refactor and optimisation on cDU
1 parent b73f0b0 commit 7d59da7

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/with-size.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable react/no-multi-comp */
22
/* eslint-disable react/require-default-props */
3+
/* eslint-disable react/no-find-dom-node */
34

45
import React, { Children, Component } from 'react'
56
import PropTypes from 'prop-types'
@@ -178,6 +179,8 @@ function withSize(config = defaultConfig) {
178179
onSize: PropTypes.func,
179180
}
180181

182+
domEl = null
183+
181184
state = {
182185
width: undefined,
183186
height: undefined,
@@ -187,14 +190,10 @@ function withSize(config = defaultConfig) {
187190
componentDidMount() {
188191
this.detector = resizeDetector(resizeDetectorStrategy)
189192
this.determineStrategy(this.props)
190-
this.handleDOMNode(true)
193+
this.handleDOMNode()
191194
}
192195

193196
componentDidUpdate() {
194-
/**
195-
* Change component will mount to componentDidUpdate
196-
* Based on https://github.com/reactjs/reactjs.org/issues/721
197-
*/
198197
this.determineStrategy(this.props)
199198
this.handleDOMNode()
200199
}
@@ -235,25 +234,29 @@ function withSize(config = defaultConfig) {
235234
strategisedGetState = () =>
236235
this.strategy === 'callback' ? this.callbackState : this.state
237236

238-
handleDOMNode(first) {
237+
handleDOMNode() {
239238
const found = this.element && ReactDOM.findDOMNode(this.element)
240239

241240
if (!found) {
242241
// If we previously had a dom node then we need to ensure that
243242
// we remove any existing listeners to avoid memory leaks.
244-
if (!first && this.domEl) {
243+
if (this.domEl) {
245244
this.detector.removeAllListeners(this.domEl)
246245
this.domEl = null
247246
}
248247
return
249248
}
250249

251-
if (!first && this.domEl) {
250+
if (!this.domEl) {
251+
this.domEl = found
252+
this.detector.listenTo(this.domEl, this.checkIfSizeChanged)
253+
} else if (!this.domEl.isSameNode(found)) {
252254
this.detector.removeAllListeners(this.domEl)
255+
this.domEl = found
256+
this.detector.listenTo(this.domEl, this.checkIfSizeChanged)
257+
} else {
258+
// Do nothing 👍
253259
}
254-
255-
this.domEl = found
256-
this.detector.listenTo(this.domEl, this.checkIfSizeChanged)
257260
}
258261

259262
refCallback = element => {
@@ -332,7 +335,7 @@ function withSize(config = defaultConfig) {
332335
* extra render cycles to happen within your components depending on the logic
333336
* contained within them around the usage of the `size` data.
334337
*
335-
* DEPRECATED: Please use the global disablePlaceholders
338+
* DEPRECATED: Please use the global noPlaceholders
336339
*/
337340
withSize.enableSSRBehaviour = false
338341

0 commit comments

Comments
 (0)