You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Function to stop the function from being invoked on every animation frame, and if that was the last function in application still running on animation frame - will effectively `cancelRequestAnimationFrame`.
283
313
284
-
**You can be assured in it's static reference - no changes between renders, same function**
314
+
**You can be assured in it's static reference - no changes between renders, same function**.
315
+
316
+
<details>
317
+
<summary><b>Example</b></summary>
285
318
319
+
<!-- prettier-ignore-start -->
286
320
```typescript
287
321
const [stop] =useAnimationFrame(fn);
288
322
@@ -291,14 +325,21 @@ useEffect(() => {
291
325
stop();
292
326
}
293
327
}, [stop]);
328
+
294
329
```
330
+
<!-- prettier-ignore-end -->
331
+
332
+
</details>
295
333
296
334
##### `[ , start]`
297
335
298
336
Function to start the function invocations on every animation frame, and if it's the first function in application to be run on animation frame - starts a single animation frame loop.
299
337
300
338
**You can be assured in it's static reference - no changes between renders, same function**
@@ -378,6 +426,9 @@ Function that accepts your `listener` function.
378
426
379
427
**You can be assured in it's static reference - no changes between renders, same function**
380
428
429
+
<details>
430
+
<summary><b>Example</b></summary>
431
+
381
432
```typescript
382
433
const fn =useCallback(() => {
383
434
returnnewDate().getTime();
@@ -399,6 +450,8 @@ useEffect(() => {
399
450
}, [addListener]);
400
451
```
401
452
453
+
</details>
454
+
402
455
##### `[ , removeListener]`
403
456
404
457
Function that accepts `listenerId: string` unique uuid from [`[addListener]`](#addlistener) and removes a listener.
@@ -407,6 +460,9 @@ Function that accepts `listenerId: string` unique uuid from [`[addListener]`](#a
407
460
408
461
**NOTE!** There is no need to removeListener as a cleanup for a component. Whole listener tree will be destroyed when component will be unmounted. Use it only when you explicitly need to remove your side effect for some matter, or if you add your listener conditionally, and want it to be conditionally removed.
You might have noticed that you could simply put your function inside `useAnimationFrame`, do side effects inside it, `start` and `stop` it when you need.
443
501
444
-
It is true, however **you should consider using `useListenOnAnimationFrame` with listeners when you want multiple side effects (or callbacks)** for your function on animation frames.
502
+
It is true, however **you should consider using `useListenOnAnimationFrame` with listeners when you want multiple side effects (or callbacks)** for your function on animation frames because of performance implications.
503
+
504
+
<details>
505
+
<summary><b>Explanation</b></summary>
445
506
446
507
Comparing
447
508
@@ -509,6 +570,8 @@ useEffect(() => {
509
570
510
571
Which will effectively call `video.currentTime` once on each animationFrame and 3 listeners to it.
511
572
573
+
</details>
574
+
512
575
## :gear: Advanced usage
513
576
514
577
### Access previous animation frame function return
@@ -517,6 +580,9 @@ If you for some reason need previous animation frame return of your function - i
517
580
518
581
[Try it on codesandbox](https://codesandbox.io/s/ms-elapsed-from-1970-pwgpft)
You can stop and start tracking again whenever you want.
@@ -565,6 +633,9 @@ You can stop and start tracking again whenever you want.
565
633
566
634
<em>[Btw, compare the above performance with `setInterval`. You couldn't achieve same smoothness when event loop is busy](https://codesandbox.io/s/interval-vs-animation-frame-065es8)</em>
By default, if you don't provide [`shouldInvokeListeners` option](#optionsshouldinvokelisteners) - listeners will be invoked only if tracked function return changes. It means that a supplied function will still be invoked on every animation frame, but listeners will not.
641
714
642
715
[Try it on codesandbox](https://codesandbox.io/s/player-timer-heavy-load-yqz79q)
0 commit comments