Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/truncateString.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ class TruncateString extends PureComponent {

resetTruncate = debounce(50, () => {
// this renders the original string so we can measure it
this.setState({truncating: true}, () => {
// now we render again with the truncated string
const truncatedString = this.getTruncateString(this.props.text)
this.setState({truncatedString, truncating: false})
})
if (this._isMounted) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One other approach here, might be to change debounce to a this.timeoutId = setTimeout(...). When the component unmounts you can just clearTimeout(this.timeoutId) so this function never fires.

this.setState({truncating: true}, () => {
// now we render again with the truncated string
const truncatedString = this.getTruncateString(this.props.text)
this.setState({truncatedString, truncating: false})
})
}
})

componentDidMount() {
Expand All @@ -79,6 +81,7 @@ class TruncateString extends PureComponent {
this.setState({truncatedString, truncating: false})

window.addEventListener('resize', this.resetTruncate)
this._isMounted = true
}

componentDidUpdate = (_, prevState) => {
Expand All @@ -92,6 +95,7 @@ class TruncateString extends PureComponent {
}

componentWillUnmount() {
this._isMounted = false
window.removeEventListener('resize', this.resetTruncate)
}

Expand Down