Skip to content

Commit f9d72a5

Browse files
committed
Throttle on React updates
1 parent ab18d44 commit f9d72a5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/App.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ import { scaleLinear } from 'd3-scale';
66

77
import Pythagoras from './Pythagoras';
88

9+
10+
// borrowed from Vue fork https://github.com/yyx990803/vue-fractal/blob/master/src/App.vue
11+
function throttleWithRAF (fn) {
12+
let running = false
13+
return function () {
14+
if (running) return
15+
running = true
16+
window.requestAnimationFrame(() => {
17+
fn.apply(this, arguments)
18+
running = false
19+
})
20+
}
21+
}
22+
923
class App extends Component {
1024
svg = {
1125
width: 1280,
@@ -17,7 +31,7 @@ class App extends Component {
1731
heightFactor: 0,
1832
lean: 0
1933
};
20-
34+
running = false;
2135
realMax = 11;
2236

2337
componentDidMount() {
@@ -35,7 +49,13 @@ class App extends Component {
3549
}
3650
}
3751

52+
// Throttling approach borrowed from Vue fork
53+
// https://github.com/yyx990803/vue-fractal/blob/master/src/App.vue
54+
// rAF makes it slower than just throttling on React update
3855
onMouseMove(event) {
56+
if (this.running) return;
57+
this.running = true;
58+
3959
const [x, y] = d3mouse(this.refs.svg),
4060

4161
scaleFactor = scaleLinear().domain([this.svg.height, 0])
@@ -48,6 +68,7 @@ class App extends Component {
4868
heightFactor: scaleFactor(y),
4969
lean: scaleLean(x)
5070
});
71+
this.running = false;
5172
}
5273

5374
render() {

0 commit comments

Comments
 (0)