CopyToClipboard: clear timeout on unmount

This commit is contained in:
Jannis R
2016-11-10 17:26:45 +01:00
parent a97e68a030
commit acbaed59c0

View File

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