Created
March 20, 2020 17:13
-
-
Save chrisryana/7a0b7fe92cb0224362b79436898ab95e to your computer and use it in GitHub Desktop.
React 1 перерисовка компонента в 16мс
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Решение проблемы производительности при событиях скролла, перетаскивания, изменениях размеров окна браузера и пр. | |
// Этот способ обеспечивает 60fps, т.е. одна перерисовка на 16 мс | |
constructor(props) { | |
super(props); | |
this.lastUpdate = +new Date(); | |
this.updateTimer = null; | |
} | |
shouldComponentUpdate(nextProps, nextState) { | |
if (+new Date() - this.lastUpdate > 16) { | |
this.lastUpdate = +new Date(); | |
clearTimeout(this.updateTimer); | |
return true; | |
} else { | |
this.updateTimer = setTimeout(() => { | |
this.forceUpdate(); | |
}, 100); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment