{"id":28315696,"url":"https://github.com/devnax/react-tsync","last_synced_at":"2025-10-13T22:41:23.268Z","repository":{"id":291748556,"uuid":"946519888","full_name":"devnax/react-tsync","owner":"devnax","description":null,"archived":false,"fork":false,"pushed_at":"2025-06-19T03:10:43.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-19T04:23:41.724Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devnax.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2025-03-11T09:14:41.000Z","updated_at":"2025-06-19T03:10:45.000Z","dependencies_parsed_at":"2025-05-06T11:24:03.237Z","dependency_job_id":"472809bb-a237-4dfa-b450-b503a720ae01","html_url":"https://github.com/devnax/react-tsync","commit_stats":null,"previous_names":["devnax/react-tsync"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/devnax/react-tsync","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Freact-tsync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Freact-tsync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Freact-tsync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Freact-tsync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devnax","download_url":"https://codeload.github.com/devnax/react-tsync/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devnax%2Freact-tsync/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017144,"owners_count":26085984,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-05-24T23:19:04.015Z","updated_at":"2025-10-13T22:41:23.261Z","avatar_url":"https://github.com/devnax.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Here's the updated documentation with the correct package name:\n\n# `react-tsync` - React Hook and HOC for Handling Asynchronous Operations\n\n## Overview\n\n`react-tsync` provides two utility functions for handling asynchronous operations in React: `useAsync` and `withAsync`. These hooks simplify the management of asynchronous data fetching, error handling, and reloading logic within React components.\n\n### Features\n\n- **`useAsync`**: A hook that handles asynchronous operations, loading states, and error handling within a component.\n- **`withAsync`**: A higher-order component (HOC) that wraps a component to handle asynchronous operations with built-in loading and error fallback UI.\n\n## Installation\n\nTo install the package, use npm or yarn:\n\n```bash\nnpm install react-tsync\n# or\nyarn add react-tsync\n```\n\n## API Documentation\n\n### `useAsync`\n\n```ts\nconst { data, isLoading, error, reload } = useAsync\u003cT\u003e(promise: Promise\u003cT\u003e, dependencies: any[] = []): UseAsyncResult\u003cT\u003e\n```\n\n#### Parameters\n\n- **`promise`** (`Promise\u003cT\u003e`): A promise that represents an asynchronous operation. The hook will execute this promise and manage the loading and error states.\n- **`dependencies`** (`any[]`, optional): An array of dependencies for the `useEffect` hook. It determines when the async operation should be re-triggered.\n\n#### Returns\n\nThe `useAsync` hook returns an object with the following properties:\n\n- **`data`** (`T | null`): The result of the asynchronous operation. If the operation hasn't completed yet, this will be `null`.\n- **`isLoading`** (`boolean`): A flag indicating whether the asynchronous operation is in progress.\n- **`error`** (`Error | null`): An error object if an error occurred during the operation. If no error occurred, this will be `null`.\n- **`reload`** (`() =\u003e void`): A function to trigger a reload of the asynchronous operation. It toggles the reloading state, re-running the promise.\n\n#### Example Usage\n\n```tsx\nconst MyComponent = () =\u003e {\n  const { data, isLoading, error, reload } = useAsync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);\n\n  if (isLoading) return \u003cdiv\u003eLoading...\u003c/div\u003e;\n  if (error) return \u003cdiv\u003eError: {error.message}\u003c/div\u003e;\n\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e{data}\u003c/div\u003e\n      \u003cbutton onClick={reload}\u003eReload\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n### `withAsync`\n\n```ts\nconst withAsync = (Component: ComponentPromise, fallback?: React.ReactNode, onError?: onError) =\u003e {\n  return function WrappedComponent\u003cP\u003e(props: P): React.ReactNode\n}\n```\n\n#### Parameters\n\n- **`Component`** (`ComponentPromise`): A function that returns a React component. This component will be wrapped by the HOC.\n- **`fallback`** (`React.ReactNode`, optional): A React node to be shown while the data is loading.\n- **`onError`** (`onError`, optional): A function that takes an error message and returns a React node. This will be displayed if an error occurs during the asynchronous operation.\n\n#### Returns\n\n- A React component that wraps the provided `Component` and handles asynchronous operations. This component will display the fallback UI while loading and handle errors using the provided `onError` function.\n\n#### Example Usage\n\n```tsx\nconst MyComponent = (props: { id: string }) =\u003e {\n  return \u003cdiv\u003eData for {props.id}\u003c/div\u003e;\n};\n\nconst WrappedComponent = withAsync(MyComponent, \u003cdiv\u003eLoading...\u003c/div\u003e, (error: string) =\u003e \u003cdiv\u003eError: {error}\u003c/div\u003e);\n\nconst App = () =\u003e {\n  return \u003cWrappedComponent id=\"123\" /\u003e;\n};\n```\n\n## Example Usage\n\n### Example 1: Using `useAsync` in a Component\n\n```tsx\nconst FetchDataComponent = () =\u003e {\n  const { data, isLoading, error, reload } = useAsync(fetch(`https://jsonplaceholder.typicode.com/posts/${id}`), []);\n\n  if (isLoading) return \u003cdiv\u003eLoading...\u003c/div\u003e;\n  if (error) return \u003cdiv\u003eError: {error.message}\u003c/div\u003e;\n\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e{data.bod}\u003c/div\u003e\n      \u003cbutton onClick={reload}\u003eReload\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n### Example 2: Using `withAsync` to Wrap a Component\n\n```tsx\nconst MyComponent = async ({ id }: { id: string }) =\u003e {\n  const data = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)\n  return \u003cdiv\u003eData for {data.body}\u003c/div\u003e;\n};\n\nconst WrappedComponent = withAsync(MyComponent, \u003cdiv\u003eLoading...\u003c/div\u003e, (error: string) =\u003e \u003cdiv\u003eError: {error}\u003c/div\u003e);\n\nconst App = () =\u003e {\n  return \u003cWrappedComponent id=\"123\" /\u003e;\n};\n```\n\n## License\n\nThis package is open-source and available under the MIT License.\n\n---\n\nYou can now publish this documentation along with your package `react-tsync` on npm!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevnax%2Freact-tsync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevnax%2Freact-tsync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevnax%2Freact-tsync/lists"}