{"id":20160600,"url":"https://github.com/reactnativecn/react-safe-promise","last_synced_at":"2025-04-10T00:10:20.951Z","repository":{"id":57343993,"uuid":"49057489","full_name":"reactnativecn/react-safe-promise","owner":"reactnativecn","description":"Wrap a promise to avoid memory leak and warning after component is unmounted","archived":false,"fork":false,"pushed_at":"2016-06-03T05:49:21.000Z","size":2,"stargazers_count":48,"open_issues_count":2,"forks_count":13,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-03-26T10:21:44.104Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/reactnativecn.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}},"created_at":"2016-01-05T09:57:15.000Z","updated_at":"2020-04-09T11:12:38.000Z","dependencies_parsed_at":"2022-09-12T06:51:08.033Z","dependency_job_id":null,"html_url":"https://github.com/reactnativecn/react-safe-promise","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactnativecn%2Freact-safe-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactnativecn%2Freact-safe-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactnativecn%2Freact-safe-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reactnativecn%2Freact-safe-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reactnativecn","download_url":"https://codeload.github.com/reactnativecn/react-safe-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131317,"owners_count":21052819,"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":[],"created_at":"2024-11-14T00:14:52.804Z","updated_at":"2025-04-10T00:10:20.928Z","avatar_url":"https://github.com/reactnativecn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Wrap a promise to avoid memory leak and warning after component is unmounted.\n\nFix a common warning that says `Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.`;\n\n## Install:\n\n```bash\nnpm install react-safe-promise -save\n```\n\n## Wrap your component:\n\nFor babel 5 (React native \u003c= 0.15), you can use decorator:\n\n```javascript\nimport safePromise from 'react-safe-promise';\n\n@safePromise\nexport default class MyComponent extends React.Component\n```\n\nFor babel 6 (React native \u003e= 0.16), you must use as a function\n\n```javascript\nimport safePromise from 'react-safe-promise';\n\nclass MyComponent extends React.Component{\n}\n\nexport default safePromise(MyComponent);\n```\n\n## Usage:\n\n```javascript\nthis.safePromise(fetch(url))\n  .then(resp=\u003ethis.safePromise(resp.json()))\n  .then(data=\u003ethis.setState({data:data}));\n```\n\nIf the promise returned by `fetch(url)` is resolved after the component was unmounted, no following callback will be called. And, the reference to component will be released quickly after the component was unmounted.\n\n## API\n\n### safePromise(Clazz)\n\nWrap a component class to support `this.safePromise()` method.\n\n### this.safePromise(promise)\n\nWrap a promise, return a new promise, which will not keep reference to this after this was unmounted.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactnativecn%2Freact-safe-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freactnativecn%2Freact-safe-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freactnativecn%2Freact-safe-promise/lists"}