Skip to content

Commit 2d5949d

Browse files
authored
fix: Attempt to measure component after it has been unmounted (#10)
* fix: Cannot get component measurements + attempt to truncatate after unmount - Added sensible defaults to ensure we return numeric values if for some reason we cannot get the measurements of the component - Cancel the debounced onResize and parseTextForTruncation when the component unmounts * fix: offsetWidth / offsetHeight of null
1 parent d47bada commit 2d5949d

File tree

3 files changed

+205
-161
lines changed

3 files changed

+205
-161
lines changed

src/react-middle-truncate/middle-truncate.jsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class MiddleTruncate extends PureComponent {
5353
start: PropTypes.oneOfType([PropTypes.number, PropTypes.instanceOf(RegExp), PropTypes.string]),
5454
style: PropTypes.object,
5555
text: PropTypes.string
56-
}
56+
};
5757

5858
static defaultProps = {
5959
className: '',
@@ -64,13 +64,23 @@ class MiddleTruncate extends PureComponent {
6464
start: 0,
6565
style: {},
6666
text: ''
67+
};
68+
69+
constructor(props) {
70+
super(props);
71+
72+
// Debounce the parsing of the text so that the component has had time to render its DOM for measurement calculations
73+
this.parseTextForTruncation = debounce(this.parseTextForTruncation.bind(this), 0);
74+
75+
// Debounce the onResize handler
76+
this.onResize = debounce(this.onResize.bind(this), props.onResizeDebounceMs);
6777
}
6878

6979
state = {
7080
truncatedText: this.props.text,
7181
start: getStartOffset(this.props.start, this.props.text),
7282
end: getEndOffset(this.props.end, this.props.text)
73-
}
83+
};
7484

7585
componentDidMount() {
7686
this.parseTextForTruncation(this.props.text);
@@ -92,6 +102,10 @@ class MiddleTruncate extends PureComponent {
92102
}
93103

94104
componentWillUnmount() {
105+
// Cancel any pending debounced functions
106+
this.onResize.cancel();
107+
this.parseTextForTruncation.cancel();
108+
95109
window.removeEventListener('resize', this.onResize);
96110
}
97111

@@ -115,9 +129,9 @@ class MiddleTruncate extends PureComponent {
115129
}
116130
}
117131

118-
onResize = debounce(() => {
132+
onResize() {
119133
this.parseTextForTruncation(this.props.text);
120-
}, this.props.onResizeDebounceMs)
134+
}
121135

122136
getTextMeasurement = ref => {
123137
const node = findDOMNode(ref);
@@ -144,7 +158,9 @@ class MiddleTruncate extends PureComponent {
144158

145159
getComponentMeasurement = () => {
146160
const node = findDOMNode(this.refs.component);
147-
const { offsetWidth, offsetHeight } = node;
161+
162+
const offsetWidth = node && node.offsetWidth ? node.offsetWidth : 0;
163+
const offsetHeight = node && node.offsetHeight ? node.offsetHeight : 0;
148164

149165
return {
150166
width: units.parse(offsetWidth, 'px'),
@@ -180,8 +196,7 @@ class MiddleTruncate extends PureComponent {
180196
return `${preserveLeftSide}${leftSide}${ellipsis}${rightSide}${preserveRightSide}`;
181197
}
182198

183-
// Debounce the parsing of the text so that the component has had time to render its DOM for measurement calculations
184-
parseTextForTruncation = debounce(text => {
199+
parseTextForTruncation(text) {
185200
const measurements = this.calculateMeasurements();
186201

187202
const truncatedText =
@@ -190,7 +205,7 @@ class MiddleTruncate extends PureComponent {
190205
: text;
191206

192207
this.setState(() => ({ truncatedText }));
193-
}, 0)
208+
}
194209

195210
render() {
196211
// eslint-disable-next-line no-unused-vars

0 commit comments

Comments
 (0)