{"id":13450574,"url":"https://github.com/f/react-wait","last_synced_at":"2025-04-12T09:23:37.268Z","repository":{"id":45754182,"uuid":"156267000","full_name":"f/react-wait","owner":"f","description":"Complex Loader Management Hook for React Applications","archived":false,"fork":false,"pushed_at":"2019-08-22T20:56:04.000Z","size":2590,"stargazers_count":306,"open_issues_count":7,"forks_count":30,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-26T18:06:14.861Z","etag":null,"topics":["hooks","loading","react","react-hooks","ui-components","ux"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/y3w5v5lk0j","language":"JavaScript","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/f.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},"funding":{"patreon":"fka"}},"created_at":"2018-11-05T18:55:01.000Z","updated_at":"2024-01-04T21:06:50.000Z","dependencies_parsed_at":"2022-07-30T13:18:12.845Z","dependency_job_id":null,"html_url":"https://github.com/f/react-wait","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-wait","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-wait/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-wait/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f%2Freact-wait/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f","download_url":"https://codeload.github.com/f/react-wait/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248544585,"owners_count":21121975,"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":["hooks","loading","react","react-hooks","ui-components","ux"],"created_at":"2024-07-31T07:00:36.266Z","updated_at":"2025-04-12T09:23:37.239Z","avatar_url":"https://github.com/f.png","language":"JavaScript","funding_links":["https://patreon.com/fka"],"categories":["Packages"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"./resources/logo.png\" width=\"500\"\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n Complex Loader Management Hook for \u003ca href=\"http://reactjs.org/\" rel=\"nofollow\" class=\"rich-diff-level-one\"\u003eReact\u003c/a\u003e.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n \u003cstrong class=\"rich-diff-level-one\"\u003eRead the \u003ca href=\"https://medium.com/@fkadev/managing-complex-waiting-experiences-on-web-uis-29534d2d92a8\" rel=\"nofollow\"\u003eMedium post \"Managing Complex Waiting Experiences on Web UIs\"\u003c/a\u003e.\u003c/strong\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"./resources/use-wait.gif\" width=\"600\" /\u003e\n\u003c/p\u003e\n\n[![npm version](https://badge.fury.io/js/react-wait.svg)](https://badge.fury.io/js/react-wait)\n[![build](https://api.travis-ci.org/f/react-wait.svg?branch=master)](https://travis-ci.org/f/react-wait)\n[![codecov](https://codecov.io/gh/f/react-wait/branch/master/graph/badge.svg)](https://codecov.io/gh/f/react-wait)\n\n---\n\n[![Edit useWait](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/y3w5v5lk0j)\n\n**react-wait** is a **React Hook** helps to manage multiple loading states on the page without any conflict. It's based on a **very simple idea** that manages an **`Array`** of multiple loading states. The **built-in loader component** listens its registered loader and immediately become loading state.\n\n## **Why not `React.Suspense`?**:\n\nReact has its own Suspense feature to manage all the async works. For now it only supports code-splitting (not data-fetching).\n\n`useWait` allows you to manage waiting experiences much more explicitly and **not only for Promised/async patterns but also complete loading management**.\n\n# Overview\n\nHere's a quick overview that what's `useWait` for:\n\n```jsx\nimport { useWait, Waiter } from \"react-wait\";\n\nfunction A() {\n  const { isWaiting } = useWait();\n  return (\n    \u003cdiv\u003e\n      {isWaiting(\"creating user\") ? \"Creating User...\" : \"Nothing happens\"}\n    \u003c/div\u003e\n  );\n}\n\nfunction B() {\n  const { anyWaiting } = useWait();\n  return (\n    \u003cdiv\u003e\n      {anyWaiting() ? \"Something happening on app...\" : \"Nothing happens\"}\n    \u003c/div\u003e\n  );\n}\n\nfunction C() {\n  const { startWaiting, endWaiting, isWaiting } = useWait();\n\n  function createUser() {\n    startWaiting(\"creating user\");\n    // Faking the async work:\n    setTimeout(() =\u003e {\n      endWaiting(\"creating user\");\n    }, 1000);\n  }\n\n  return (\n    \u003cbutton disabled={isWaiting(\"creating user\")} onClick={createUser}\u003e\n      \u003cWait on=\"creating user\" fallback={\u003cSpinner /\u003e}\u003e\n        Create User\n      \u003c/Wait\u003e\n    \u003c/button\u003e\n  );\n}\n\nReactDOM.render(\n  \u003cWaiter\u003e\n    \u003cC /\u003e\n  \u003c/Waiter\u003e,\n  document.getElementById(\"root\")\n);\n```\n\n# Quick Start\n\nIf you are a **try and learn** developer, you can start trying the **react-wait** now using [codesandbox.io](https://codesandbox.io).\n\n[Quick start on CodeSandbox](https://codesandbox.io/s/y3w5v5lk0j)\n\n### 1. Install:\n\n```bash\nyarn add react-wait\n```\n\n### 2. Require:\n\n```jsx\nimport { Waiter, useWait } from \"react-wait\";\n\nfunction UserCreateButton() {\n  const { startWaiting, endWaiting, isWaiting, Wait } = useWait();\n\n  return (\n    \u003cbutton\n      onClick={() =\u003e startWaiting(\"creating user\")}\n      disabled={isWaiting(\"creating user\")}\n    \u003e\n      \u003cWait on=\"creating user\" fallback={\u003cdiv\u003eCreating user!\u003c/div\u003e}\u003e\n        Create User\n      \u003c/Wait\u003e\n    \u003c/button\u003e\n  );\n}\n```\n\n### 3. Wrap with the `Waiter` Context Provider\n\nAnd you should wrap your `App` with `Waiter` component. It's actually a `Context.Provider` that provides a loading context to the component tree.\n\n```jsx\nconst rootElement = document.getElementById(\"root\");\nReactDOM.render(\n  \u003cWaiter\u003e\n    \u003cApp /\u003e\n  \u003c/Waiter\u003e,\n  rootElement\n);\n```\n\n## Installation\n\n```bash\n$ yarn add react-wait\n# or if you using npm\n$ npm install react-wait\n```\n\n## The API\n\n**react-wait** provides some helpers to you to use in your templates.\n\n#### `anyWaiting()`\n\nReturns boolean value if any loader exists in context.\n\n```jsx\nconst { anyWaiting } = useWait();\n\nreturn \u003cbutton disabled={anyWaiting()}\u003eDisabled while waiting\u003c/button\u003e;\n```\n\n#### `isWaiting(waiter String)`\n\nReturns boolean value if given loader exists in context.\n\n```jsx\nconst { isWaiting } = useWait();\n\nreturn (\n  \u003cbutton disabled={isWaiting(\"creating user\")}\u003e\n    Disabled while creating user\n  \u003c/button\u003e\n);\n```\n\n#### `startWaiting(waiter String)`\n\nStarts the given waiter.\n\n```jsx\nconst { startWaiting } = useWait();\n\nreturn \u003cbutton onClick={() =\u003e startWaiting(\"message\")}\u003eStart\u003c/button\u003e;\n```\n\n#### `endWaiting(waiter String)`\n\nStops the given waiter.\n\n```jsx\nconst { end } = useWait();\n\nreturn \u003cbutton onClick={() =\u003e endWaiting(\"message\")}\u003eStop\u003c/button\u003e;\n```\n\n## Using `Wait` Component\n\n```jsx\nfunction Component() {\n  const { Wait } = useWait();\n  return (\n    \u003cWait on=\"the waiting message\" fallback={\u003cdiv\u003eWaiting...\u003c/div\u003e}\u003e\n      The content after waiting done\n    \u003c/Wait\u003e\n  );\n}\n```\n\nBetter example for a `button` with loading state:\n\n```jsx\n\u003cbutton disabled={isWaiting(\"creating user\")}\u003e\n  \u003cWait on=\"creating user\" fallback={\u003cdiv\u003eCreating User...\u003c/div\u003e}\u003e\n    Create User\n  \u003c/Wait\u003e\n\u003c/button\u003e\n```\n\n## Making Reusable Loader Components\n\nWith reusable loader components, you will be able to use custom loader components as example below. This will allow you to create better **user loading experience**.\n\n```jsx\nfunction Spinner() {\n  return \u003cimg src=\"spinner.gif\" /\u003e;\n}\n```\n\nNow you can use your spinner everywhere using `waiting` attribute:\n\n```jsx\n\u003cbutton disabled={isWaiting(\"creating user\")}\u003e\n  \u003cWait on=\"creating user\" fallback={\u003cSpinner /\u003e}\u003e\n    Create User\n  \u003c/Wait\u003e\n\u003c/button\u003e\n```\n\n## Creating Waiting Contexts using `createWaitingContext(context String)`\n\nTo keep your code DRY you can create a `Waiting Context` using `createWaitingContext`.\n\n```jsx\nfunction CreateUserButton() {\n  const { createWaitingContext } = useWait();\n\n  // All methods will be curried with \"creating user\" on.\n  const { startWaiting, endWaiting, isWaiting, Wait } = createWaitingContext(\n    \"creating user\"\n  );\n\n  function createUser() {\n    startWaiting();\n    setTimeout(endWaiting, 1000);\n  }\n\n  return (\n    \u003cButton disabled={isWaiting()} onClick={createUser}\u003e\n      \u003cWait fallback=\"Creating User...\"\u003eCreate User\u003c/Wait\u003e\n    \u003c/Button\u003e\n  );\n}\n```\n\n## Contributors\n\n- Fatih Kadir Akın, (creator)\n\n## Other Implementations\n\nSince **react-wait** based on a very simple idea, it can be implemented on other frameworks.\n\n- [vue-wait](https://github.com/f/vue-wait): Multiple Process Loader Management for Vue.\n- [dom-wait](https://github.com/f/dom-wait): Multiple Process Loader Management for vanilla JavaScript.\n\n## License\n\nMIT © [Fatih Kadir Akın](https://github.com/f)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff%2Freact-wait","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff%2Freact-wait","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff%2Freact-wait/lists"}