|
1 |
| -import { EmbeddedEditor } from '@site/src/components/Editor/EmbeddedEditor'; |
2 |
| -import ExampleApp from '!!raw-loader!../../src/examples/useTick/App'; |
3 |
| -import ExampleAppMemoized from '!!raw-loader!../../src/examples/useTick-memoized/App'; |
4 |
| -import ExampleAppUnmemoized from '!!raw-loader!../../src/examples/useTick-unmemoized/App'; |
5 |
| -import ExampleAppWithOptions from '!!raw-loader!../../src/examples/useTick-options/App'; |
6 |
| - |
7 | 1 | `useTick` allows a callback to be attached to the [`Ticker`][pixi-ticker] on the parent application.
|
8 | 2 |
|
9 |
| -<EmbeddedEditor |
10 |
| - files={{ 'App.js': ExampleApp }} |
11 |
| - viewType={'editor'} |
12 |
| - width={'100%'} /> |
| 3 | +```jsx |
| 4 | +import { |
| 5 | + Application, |
| 6 | + useTick, |
| 7 | +} from '@pixi/react'; |
| 8 | + |
| 9 | +const ChildComponent = () => { |
| 10 | + useTick(() => console.log('This will be logged on every tick')); |
| 11 | +}; |
| 12 | + |
| 13 | +const MyComponent = () => ( |
| 14 | + <Application> |
| 15 | + <ChildComponent /> |
| 16 | + </Application> |
| 17 | +); |
| 18 | +``` |
13 | 19 |
|
14 | 20 | `useTick` can also accept an options object. This allows control of all [`ticker.add`][pixi-ticker-add] options, as well as adding the `isEnabled` option. Setting `isEnabled` to `false` will cause the callback to be disabled until the argument is changed to true again.
|
15 | 21 |
|
16 |
| -<EmbeddedEditor |
17 |
| - files={{ 'App.js': ExampleAppWithOptions }} |
18 |
| - viewType={'editor'} |
19 |
| - width={'100%'} /> |
| 22 | +```jsx |
| 23 | +import { |
| 24 | + Application, |
| 25 | + useTick, |
| 26 | +} from '@pixi/react'; |
| 27 | +import { UPDATE_PRIORITY } from 'pixi.js' |
| 28 | +import { useRef } from 'react' |
| 29 | + |
| 30 | +const ChildComponent = () => { |
| 31 | + const spriteRef = useRef(null) |
| 32 | + |
| 33 | + useTick({ |
| 34 | + callback() { |
| 35 | + // this === context |
| 36 | + this.current.rotation += 1 |
| 37 | + }, |
| 38 | + context: spriteRef, |
| 39 | + isEnabled: true, |
| 40 | + priority: UPDATE_PRIORITY.HIGH, |
| 41 | + }) |
| 42 | + |
| 43 | + return <pixiSprite ref={spriteRef} /> |
| 44 | +}; |
| 45 | + |
| 46 | +const MyComponent = () => ( |
| 47 | + <Application> |
| 48 | + <ChildComponent /> |
| 49 | + </Application> |
| 50 | +); |
| 51 | +``` |
20 | 52 |
|
21 | 53 | ## ⚠️ WARNING ⚠️
|
22 | 54 |
|
23 | 55 | The callback passed to `useTick` **is not memoised**. This can cause issues where your callback is being removed and added back to the ticker on every frame if you're mutating state in a component where `useTick` is using a non-memoised function. For example, this issue would affect the component below because we are mutating the state, causing the component to re-render constantly:
|
24 | 56 |
|
25 |
| -<EmbeddedEditor |
26 |
| - files={{ 'App.js': ExampleAppUnmemoized }} |
27 |
| - viewType={'editor'} |
28 |
| - width={'100%'} /> |
| 57 | +```jsx |
| 58 | +import { |
| 59 | + Application, |
| 60 | + useTick, |
| 61 | +} from '@pixi/react'; |
| 62 | +import { useState } from 'react' |
| 63 | + |
| 64 | +const ChildComponent = () => { |
| 65 | + const [rotation, setRotation] = useState(0) |
| 66 | + |
| 67 | + useTick(() => setRotation(previousState => previousState + 1)); |
| 68 | + |
| 69 | + return <pixiSprite rotation={rotation} />; |
| 70 | +}; |
| 71 | + |
| 72 | +const MyComponent = () => ( |
| 73 | + <Application> |
| 74 | + <ChildComponent /> |
| 75 | + </Application> |
| 76 | +); |
| 77 | +``` |
29 | 78 |
|
30 | 79 | This issue can be solved by memoising the callback passed to `useTick`:
|
31 | 80 |
|
32 |
| -<EmbeddedEditor |
33 |
| - files={{ 'App.js': ExampleAppMemoized }} |
34 |
| - viewType={'editor'} |
35 |
| - width={'100%'} /> |
| 81 | +```jsx |
| 82 | +import { |
| 83 | + Application, |
| 84 | + useTick, |
| 85 | +} from '@pixi/react'; |
| 86 | +import { useCallback, useState } from 'react' |
| 87 | + |
| 88 | +const ChildComponent = () => { |
| 89 | + const [rotation, setRotation] = useState(0) |
| 90 | + |
| 91 | + const animateRotation = useCallback(() => setRotation(previousState => previousState + 1), []); |
| 92 | + |
| 93 | + useTick(animateRotation); |
| 94 | + |
| 95 | + return <pixiSprite rotation={rotation} />; |
| 96 | +}; |
| 97 | + |
| 98 | +const MyComponent = () => ( |
| 99 | + <Application> |
| 100 | + <ChildComponent /> |
| 101 | + </Application> |
| 102 | +); |
| 103 | +``` |
36 | 104 |
|
37 | 105 | [pixi-ticker]: https://pixijs.download/release/docs/ticker.Ticker.html
|
38 | 106 | [pixi-ticker-add]: https://pixijs.download/release/docs/ticker.Ticker.html#add
|
0 commit comments