|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +<img width="100" height="100" src="media/react-logo.svg" alt="React logo"> |
| 4 | + |
| 5 | +<h1>use-transition-effect</h1> |
| 6 | +<p>Run long effects without blocking the main thread</p> |
| 7 | + |
| 8 | +[](https://www.npmjs.com/package/use-transition-effect) |
| 9 | +[](https://github.com/piotr-oles/use-transition-effect/actions?query=branch%3Amain+event%3Apush) |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +## Motivation |
| 14 | + |
| 15 | +Let's say you want to render something complex on a canvas in a React application. |
| 16 | +Canvas API is imperative, so to interact with it, you need to use the `useEffect()` hook. |
| 17 | +Unfortunately, if rendering takes too long, you can block the main thread and make your |
| 18 | +application unresponsive (especially on low-end devices). |
| 19 | + |
| 20 | +The `useTransitionEffect()` hook provides a way to split long-running effects into smaller chunks |
| 21 | +to unblock the main thread. It uses [scheduler](https://www.npmjs.com/package/scheduler) package (from React) |
| 22 | +to schedule smaller units of work and coordinate it with React rendering. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +This package requires [React 17+](https://www.npmjs.com/package/react) and [scheduler 0.20+](https://www.npmjs.com/package/scheduler) |
| 27 | + |
| 28 | +```sh |
| 29 | +# with npm |
| 30 | +npm install use-transition-effect |
| 31 | + |
| 32 | +# with yarn |
| 33 | +yarn add use-transition-effect |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +```typescript |
| 39 | +const [ |
| 40 | + isPending, |
| 41 | + startTransitionEffect, |
| 42 | + stopTransitionEffect, |
| 43 | +] = useTransitionEffect(); |
| 44 | +``` |
| 45 | + |
| 46 | +The API is very similar to the `useTransition` hook from React. |
| 47 | +It returns a stateful value for the pending state of the transition effect, a function to start it, and a function to stop it. |
| 48 | + |
| 49 | +`startTransitionEffect` lets you schedule a long-running effect without blocking the main thread. It expects a generator |
| 50 | +function as an argument, so you can yield to unblock the main thread. The generator function receives the `shouldYield` function, |
| 51 | +which returns true if the current task takes too long: |
| 52 | + |
| 53 | +```typescript |
| 54 | +startTransitionEffect(function*(shouldYield) { |
| 55 | + for (let item of items) { |
| 56 | + doSomeComplexSideEffects(item); |
| 57 | + if (shouldYield()) { |
| 58 | + yield; |
| 59 | + } |
| 60 | + } |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +Additionally, you can yield and return a cleanup function that will run on transition stop (including unmount): |
| 65 | +```typescript |
| 66 | +startTransitionEffect(function*(shouldYield) { |
| 67 | + const cleanup = () => cleanupSideEffects(); |
| 68 | + |
| 69 | + for (let item of items) { |
| 70 | + doSomeComplexSideEffects(item); |
| 71 | + if (shouldYield()) { |
| 72 | + yield cleanup; |
| 73 | + } |
| 74 | + } |
| 75 | + return cleanup; |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +`stopTransitionEffect` lets you stop the current long-running effect. You can use it as a `useEffect` cleanup: |
| 80 | + |
| 81 | +```typescript |
| 82 | +useEffect(() => { |
| 83 | + startTransitionEffect(function*() { |
| 84 | + // effect |
| 85 | + }); |
| 86 | + |
| 87 | + return () => stopTransitionEffect(); |
| 88 | +}, []); |
| 89 | +``` |
| 90 | + |
| 91 | +`isPending` indicates when a transition effect is active to show a pending state: |
| 92 | + |
| 93 | +```tsx |
| 94 | +function App() { |
| 95 | + const [ |
| 96 | + isPending, |
| 97 | + startTransitionEffect, |
| 98 | + stopTransitionEffect, |
| 99 | + ] = useTransitionEffect(); |
| 100 | + |
| 101 | + function handleStartClick() { |
| 102 | + startTransitionEffect(function*() { |
| 103 | + // do stuff, for example render something on a canvas |
| 104 | + }); |
| 105 | + } |
| 106 | + function handleStopClick() { |
| 107 | + stopTransitionEffect(); |
| 108 | + } |
| 109 | + |
| 110 | + return ( |
| 111 | + <div> |
| 112 | + {isPending && <Spinner />} |
| 113 | + <button onClick={handleStartClick} disabled={isPending}> |
| 114 | + Start |
| 115 | + </button> |
| 116 | + <button onClick={handleStopClick} disabled={!isPending}> |
| 117 | + Stop |
| 118 | + </button> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +MIT |
0 commit comments