Skip to content

Commit fc2b48f

Browse files
committed
fix(Performance): stop raf loop before unmounting
fix #47
1 parent 41fcfc9 commit fc2b48f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Confetti.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export class Confetti {
121121
context: CanvasRenderingContext2D
122122
_options!: IConfettiOptions
123123
generator: ParticleGenerator
124+
rafId?: number
124125

125126
get options(): Partial<IConfettiOptions> {
126127
return this._options
@@ -162,11 +163,19 @@ export class Confetti {
162163
context.clearRect(0, 0, canvas.width, canvas.height)
163164
}
164165
if(this.generator.animate()) {
165-
requestAnimationFrame(this.update)
166+
this.rafId = requestAnimationFrame(this.update)
166167
} else {
167168
this._options.run = false
168169
}
169170
}
171+
172+
stop = () => {
173+
this.options = { run: false }
174+
if(this.rafId) {
175+
cancelAnimationFrame(this.rafId)
176+
this.rafId = undefined
177+
}
178+
}
170179
}
171180

172181
export default Confetti

src/ReactConfetti.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export class ReactConfetti extends Component<Props> {
2424
}
2525
}
2626
componentWillUnmount() {
27+
if(this.confetti) {
28+
this.confetti.stop()
29+
}
2730
this.confetti = undefined
2831
}
2932

0 commit comments

Comments
 (0)