Skip to content

Fix for quick unmount and remounts #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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