CopyToClipboard: clear timeout on unmount

This commit is contained in:
Jannis R 2016-11-10 17:26:45 +01:00
parent a97e68a030
commit acbaed59c0
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5

View File

@ -39,9 +39,17 @@ export default class CopyToClipboard extends Component {
}; };
state = { state = {
copied: false copied: false,
timeout: null
}; };
componentWillUnmount () {
const { timeoutId } = this.state;
if (timeoutId) {
window.clearTimeout(timeoutId);
}
}
render () { render () {
const { data, label, size } = this.props; const { data, label, size } = this.props;
const { copied } = this.state; const { copied } = this.state;
@ -65,11 +73,12 @@ export default class CopyToClipboard extends Component {
onCopy = () => { onCopy = () => {
const { cooldown, onCopy } = this.props; const { cooldown, onCopy } = this.props;
this.setState({ copied: true }); this.setState({
setTimeout(() => { copied: true,
this.setState({ copied: false }); timeout: setTimeout(() => {
}, cooldown); this.setState({ copied: false, timeout: null });
}, cooldown)
});
onCopy(); onCopy();
} }
} }