{"id":21404768,"url":"https://github.com/fakundo/react-fetch-decorator","last_synced_at":"2026-04-11T02:53:06.976Z","repository":{"id":74039938,"uuid":"431614952","full_name":"fakundo/react-fetch-decorator","owner":"fakundo","description":"React Higher-Order Component (HOC) for observing and invoking fetch actions","archived":false,"fork":false,"pushed_at":"2021-11-25T14:49:13.000Z","size":287,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-24T19:49:34.151Z","etag":null,"topics":["action","fetch","react"],"latest_commit_sha":null,"homepage":"https://fakundo.github.io/react-fetch-decorator/","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/fakundo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2021-11-24T20:08:43.000Z","updated_at":"2021-11-25T14:48:56.000Z","dependencies_parsed_at":"2023-03-11T16:23:57.552Z","dependency_job_id":null,"html_url":"https://github.com/fakundo/react-fetch-decorator","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"3b8d7b563efd42714776128bfd0ce6eb65475566"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-fetch-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-fetch-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-fetch-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fakundo%2Freact-fetch-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fakundo","download_url":"https://codeload.github.com/fakundo/react-fetch-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243902291,"owners_count":20366259,"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":["action","fetch","react"],"created_at":"2024-11-22T16:18:00.784Z","updated_at":"2026-04-11T02:53:01.930Z","avatar_url":"https://github.com/fakundo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-fetch-decorator\n\n[![npm](https://img.shields.io/npm/v/react-fetch-decorator.svg)](https://www.npmjs.com/package/react-fetch-decorator)\n\nReact Higher-Order Component (HOC) for observing and invoking `fetch` requests.\n\nFeatures:\n- can rerender component when request status changes\n- can abort requests when unmounting component\n- can abort request on second call\n\n### Installation\n\n```\nnpm i react-fetch-decorator\n```\n\n### Demo\n\n[Demo](https://fakundo.github.io/react-fetch-decorator/) \n/ \n[Source](https://github.com/fakundo/react-fetch-decorator/tree/master/examples)\n\n### Usage\n\n```js\nimport React, { Component } from 'react'\nimport PropTypes from 'prop-types'\nimport withActions from 'react-fetch-decorator'\n\nclass Dog extends Component {\n  static propTypes = {\n    fetchDogAction: PropTypes.object.isRequired,\n  }\n\n  handleClick = () =\u003e {\n    const { fetchDogAction } = this.props\n    fetchDogAction.run()\n  }\n\n  render() {\n    const { fetchDogAction } = this.props\n    const { isPending, isFailed, isSucceded, error, result } = fetchDogAction\n    return (\n      \u003c\u003e\n        \u003cbutton onClick={this.handleClick}\u003e\n          Random Dog\n        \u003c/button\u003e\n\n        {isPending \u0026\u0026 (\n          \u003cdiv\u003e\n            Loading...\n          \u003c/div\u003e\n        )}\n\n        {isFailed \u0026\u0026 (\n          \u003cdiv\u003e\n            {error.message}\n          \u003c/div\u003e\n        )}\n\n        {isSucceded \u0026\u0026 (\n          \u003cimg src={result.message} /\u003e\n        )}\n      \u003c/\u003e\n    )\n  }\n}\n\nconst mapActionsToProps = {\n  fetchDogAction: async (signal) =\u003e {\n    const url = 'https://dog.ceo/api/breeds/image/random'\n    const response =  await fetch(url, { signal })\n    return response.json()\n  }\n}\n\nexport default withActions(mapActionsToProps)(Dog)\n```\n\n#### Action `run` method returns a Promise\n\n```js\n...\n\nhandleClick = async () =\u003e {\n  const { fetchDogAction } = this.props\n  try {\n    const dog = await fetchDogAction.run()\n    console.log('Fetched dog: ', dog)\n  }\n  catch (error) {\n    console.log('Error occured while fetching a dog: ', error.message)\n  }\n}\n\n...\n```\n\n#### Invoking action with parameters\n\n```js\n...\n\nconst mapActionsToProps = {\n  fetchUserAction: (signal) =\u003e async (userId) =\u003e {\n    const url = `/users/${userId}`\n    const response = await fetch(url, { signal })\n    return response.json()\n  }\n}\n\n...\n\nfetchUserAction.run({\n  args: [userId]\n})\n\n...\n```\n\n## API\n\n#### HOC creator params\n\n- `mapActionsToProps = { ... }` - function or object that defines actions. If it's a function then component props will be passed to it.\n- `options = { abortPendingOnUnmount: true }`\n\n#### Structure of action object\n\n- `isDefault` - `true` if action never ran\n- `isPending` - `true` if action is pending\n- `isSucceded` - `true` if action has succeded\n- `isFailed` - `true` if action has failed\n- `result` - result of action\n- `error` - error occured while performing action\n- `run(runOptions)` - starts action\n- `reset(resetOptions)` - resets action\n\n#### `runOptions`\n\n- `args = []` - arguments will be passed to action\n- `silent = false` - disables throwing errors\n- `abortPending = true` - aborts previous action if it still running\n- `updateComponent = true` - allows to rerender component on status change\n- `updateComponentOnPending = undefined`\n- `updateComponentOnSuccess = undefined`\n- `updateComponentOnFailure = undefined`\n\n#### `resetOptions`\n\n- `abortPending = true` - aborts pending action\n- `updateComponent = true` - invokes component rerender\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffakundo%2Freact-fetch-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffakundo%2Freact-fetch-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffakundo%2Freact-fetch-decorator/lists"}