{"id":16812038,"url":"https://github.com/piotr-oles/use-transition-effect","last_synced_at":"2025-04-06T12:09:07.243Z","repository":{"id":37697791,"uuid":"499005743","full_name":"piotr-oles/use-transition-effect","owner":"piotr-oles","description":"Run long effects without blocking the main thread","archived":false,"fork":false,"pushed_at":"2024-12-30T07:29:25.000Z","size":337,"stargazers_count":52,"open_issues_count":4,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T10:09:01.574Z","etag":null,"topics":["concurrent","effect","generator","hook","long-task","main-thread","react","transition","yield"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotr-oles.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"piotr-oles"}},"created_at":"2022-06-02T05:40:18.000Z","updated_at":"2025-01-17T22:08:37.000Z","dependencies_parsed_at":"2023-02-09T23:45:28.696Z","dependency_job_id":"2f2a16b0-b06a-496d-b2c3-76b7288b3bb4","html_url":"https://github.com/piotr-oles/use-transition-effect","commit_stats":{"total_commits":12,"total_committers":2,"mean_commits":6.0,"dds":0.08333333333333337,"last_synced_commit":"9f28103446e5e6788f45df6c7c0f41d4f0deb08c"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fuse-transition-effect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fuse-transition-effect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fuse-transition-effect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fuse-transition-effect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotr-oles","download_url":"https://codeload.github.com/piotr-oles/use-transition-effect/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478323,"owners_count":20945266,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrent","effect","generator","hook","long-task","main-thread","react","transition","yield"],"created_at":"2024-10-13T10:20:28.242Z","updated_at":"2025-04-06T12:09:07.221Z","avatar_url":"https://github.com/piotr-oles.png","language":"TypeScript","funding_links":["https://github.com/sponsors/piotr-oles"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003cimg width=\"100\" height=\"100\" src=\"media/react-logo.svg\" alt=\"React logo\"\u003e\n\n\u003ch1\u003euse-transition-effect\u003c/h1\u003e\n\u003cp\u003eRun long effects without blocking the main thread\u003c/p\u003e\n\n[![npm version](https://img.shields.io/npm/v/use-transition-effect.svg)](https://www.npmjs.com/package/use-transition-effect)\n[![build status](https://github.com/piotr-oles/use-transition-effect/workflows/CI/badge.svg?branch=main\u0026event=push)](https://github.com/piotr-oles/use-transition-effect/actions?query=branch%3Amain+event%3Apush)\n\n\u003c/div\u003e\n\n## Motivation\n\nLet's say you want to render something complex on a canvas in a React application.\nCanvas API is imperative, so to interact with it, you need to use the `useEffect()` hook.\nUnfortunately, if rendering takes too long, you can block the main thread and make your\napplication unresponsive (especially on low-end devices).\n\nThe `useTransitionEffect()` hook provides a way to split long-running effects into smaller chunks\nto unblock the main thread. It uses [scheduler](https://www.npmjs.com/package/scheduler) package (from React)\nto schedule smaller units of work and coordinate it with React rendering.\n\nI wrote an article about this on Medium: **[Non-blocking `\u003ccanvas /\u003e` Rendering in Concurrent React](https://levelup.gitconnected.com/non-blocking-canvas-rendering-with-concurrent-react-f46032b03efa)**\n\n## Installation\n\nThis package requires [React 17+](https://www.npmjs.com/package/react) and [scheduler 0.20+](https://www.npmjs.com/package/scheduler)\n\n```sh\n# with npm\nnpm install use-transition-effect\n\n# with yarn\nyarn add use-transition-effect\n```\n\n## Usage\n\n```typescript\nconst [isPending, startTransitionEffect, stopTransitionEffect] =\n  useTransitionEffect();\n```\n\nThe API is very similar to the `useTransition` hook from React.\nIt returns a stateful value for the pending state of the transition effect, a function to start it, and a function to stop it.\n\n`startTransitionEffect` lets you schedule a long-running effect without blocking the main thread. It expects a generator\nfunction as an argument, so you can yield to unblock the main thread:\n\n```typescript\nstartTransitionEffect(function* () {\n  for (let item of items) {\n    doSomeComplexSideEffects(item);\n    yield;\n  }\n});\n```\n\nAdditionally, you can yield and return a cleanup function that will run on transition stop (including unmount):\n\n```typescript\nstartTransitionEffect(function* () {\n  const cleanup = () =\u003e cleanupSideEffects();\n\n  for (let item of items) {\n    doSomeComplexSideEffects(item);\n    yield cleanup;\n  }\n  return cleanup;\n});\n```\n\n`stopTransitionEffect` lets you stop the current long-running effect. You can use it as a `useEffect` cleanup:\n\n```typescript\nuseEffect(() =\u003e {\n  startTransitionEffect(function* () {\n    // effect\n  });\n\n  return () =\u003e stopTransitionEffect();\n}, []);\n```\n\n`isPending` indicates when a transition effect is active to show a pending state:\n\n```tsx\nfunction App() {\n  const [isPending, startTransitionEffect, stopTransitionEffect] =\n    useTransitionEffect();\n\n  function handleStartClick() {\n    startTransitionEffect(function* () {\n      // do stuff, for example render something on a canvas\n    });\n  }\n  function handleStopClick() {\n    stopTransitionEffect();\n  }\n\n  return (\n    \u003cdiv\u003e\n      {isPending \u0026\u0026 \u003cSpinner /\u003e}\n      \u003cbutton onClick={handleStartClick} disabled={isPending}\u003e\n        Start\n      \u003c/button\u003e\n      \u003cbutton onClick={handleStopClick} disabled={!isPending}\u003e\n        Stop\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\nThe `scheduler` package exports the `unstable_shouldYield()` function that returns true if the current task takes too long.\nYou can use it to decide when to yield:\n\n```typescript\nimport { unstable_shouldYield as shouldYield } from \"scheduler\";\n\nstartTransitionEffect(function* () {\n  for (let item of items) {\n    doSomeComplexSideEffects(item);\n    if (shouldYield()) {\n      yield;\n    }\n  }\n});\n```\n\nIf you want to update the state during a transition effect, you have to wrap this update with the `unstable_runWithPriority()` function\nfrom the `scheduler` package (with a priority higher than `IdlePriority`). Otherwise, the state update inside the transition effect will run when the effect ends:\n\n```typescript\nimport {\n  unstable_runWithPriority as runWithPriority,\n  unstable_NormalPriority as NormalPriority,\n} from \"scheduler\";\n\nstartTransitionEffect(function* () {\n  for (let item of items) {\n    runWithPriority(NormalPriority, () =\u003e {\n      setCount((prevCount) =\u003e prevCount + 1);\n    });\n    yield;\n  }\n});\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotr-oles%2Fuse-transition-effect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotr-oles%2Fuse-transition-effect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotr-oles%2Fuse-transition-effect/lists"}