Skip to content

Commit 3bc5bf0

Browse files
authored
Merge pull request #47 from pc035860/fix-raf-async
Fix async this.forceUpdate() issue caused by requestAnimationFrame delay
2 parents e791475 + ec70520 commit 3bc5bf0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/TextTruncate.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React, {Component} from 'react';
22
import PropTypes from 'prop-types';
33

4+
const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
5+
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
6+
const cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
7+
48
export default class TextTruncate extends Component {
59
static propTypes = {
610
containerClassName: PropTypes.string,
@@ -36,10 +40,16 @@ export default class TextTruncate extends Component {
3640

3741
componentWillUnmount() {
3842
window.removeEventListener('resize', this.onResize);
43+
if (this.rafId) {
44+
cancelAnimationFrame(this.rafId);
45+
}
3946
}
4047

4148
onResize = () => {
42-
window.requestAnimationFrame(this.update.bind(this))
49+
if (this.rafId) {
50+
cancelAnimationFrame(this.rafId);
51+
}
52+
this.rafId = requestAnimationFrame(this.update.bind(this))
4353
};
4454

4555
update = () => {

0 commit comments

Comments
 (0)