{"id":13450785,"url":"https://github.com/helderberto/use-is-mounted-ref","last_synced_at":"2025-10-17T21:17:35.888Z","repository":{"id":37088973,"uuid":"263476527","full_name":"helderberto/use-is-mounted-ref","owner":"helderberto","description":"📦   useIsMountedRef() is a React Hook to check when the component is mounted.","archived":false,"fork":false,"pushed_at":"2024-04-12T13:06:22.000Z","size":5197,"stargazers_count":43,"open_issues_count":10,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-13T13:44:42.780Z","etag":null,"topics":["hook","hooks","mounted","react","react-hook","react-ref"],"latest_commit_sha":null,"homepage":"","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/helderberto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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}},"created_at":"2020-05-12T23:34:12.000Z","updated_at":"2024-04-18T12:51:20.360Z","dependencies_parsed_at":"2023-02-11T00:30:25.880Z","dependency_job_id":"48666efe-ea42-4e48-a300-429121674274","html_url":"https://github.com/helderberto/use-is-mounted-ref","commit_stats":null,"previous_names":["helderburato/use-is-mounted-ref"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helderberto%2Fuse-is-mounted-ref","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helderberto%2Fuse-is-mounted-ref/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helderberto%2Fuse-is-mounted-ref/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helderberto%2Fuse-is-mounted-ref/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helderberto","download_url":"https://codeload.github.com/helderberto/use-is-mounted-ref/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244907420,"owners_count":20529850,"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":["hook","hooks","mounted","react","react-hook","react-ref"],"created_at":"2024-07-31T07:00:38.478Z","updated_at":"2025-10-17T21:17:35.834Z","avatar_url":"https://github.com/helderberto.png","language":"JavaScript","funding_links":[],"categories":["Packages","JavaScript"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003e📦 use-is-mounted-ref\u003c/h1\u003e\n\n  \u003cp\u003e\u003cstrong\u003euseIsMountedRef is a React Hook\u003c/strong\u003e to check when the component is mounted.\u003c/p\u003e\n\n\u003c!-- prettier-ignore-start --\u003e\n[![build][build-badge]][build]\n[![version][version-badge]][package]\n[![MIT License][license-badge]][license]\n[![downloads][downloads-badge]][npmtrends]\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c/div\u003e\n\n---\n\n## Table of Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Motivation](#motivation)\n- [Usage](#usage)\n- [Component Lifecycle Overview](#component-lifecycle-overview)\n- [Contributing](#contributing)\n- [Bugs and Sugestions](#bugs-and-sugestions)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Motivation\n\n- Avoid memory leaks setting states when component are unmounted;\n- Control when component already mounted;\n- Common error when setting state to unmounted component:\n\n```js\nWarning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.\n```\n\n## Usage\n\nTo start using the `use-is-mounted-ref` in your project, first install in your project:\n\n`yarn add use-is-mounted-ref` or `npm install use-is-mounted-ref`\n\n\u003cdetails open\u003e\n\u003csummary\u003e\u003cstrong\u003eAvoid set state when unmounted component:\u003c/strong\u003e\u003c/summary\u003e\n\n```jsx\nimport { useState, useEffect } from 'react';\nimport useIsMountedRef from 'use-is-mounted-ref';\n\nfunction App() {\n  const isMountedRef = useIsMountedRef();\n\n  const initialState = {\n    loading: false,\n    error: false,\n    data: [],\n  };\n\n  const [state, setState] = useState(initialState);\n\n  useEffect(() =\u003e {\n    fetch('https://www.reddit.com/.json')\n      .then((response) =\u003e response.json())\n      .then(({ data }) =\u003e {\n        if (isMountedRef.current) {\n          setState((prevState) =\u003e {\n            return {\n              ...prevState,\n              loading: false,\n              data,\n            };\n          });\n        }\n      })\n      .catch((err) =\u003e {\n        if (isMountedRef.current) {\n          setState((prevState) =\u003e {\n            return { ...prevState, loading: false, error: true };\n          });\n        }\n      });\n  }, []);\n\n  return state.loading ? 'Loading...' : 'Found Data!';\n}\n\nexport default App;\n```\n\n\u003c/details\u003e\n\n## Component Lifecycle Overview\n\n```mermaid\nflowchart TD\n    subgraph \"Component Lifecycle\"\n        direction TB\n        A1[\"Component Mounted\"] --\u003e A2[\"useIsMountedRef Hook\"]\n        A2 --\u003e B1[\"useRef(false)\"]\n        B1 --\u003e C1[\"useEffect Hook\"]\n        C1 --\u003e D1[\"isMountedRef.current = true\"]\n        \n        C1 --\u003e E1[\"Component Unmounted\"]\n        E1 --\u003e F1[\"Cleanup function\"]\n        F1 --\u003e G1[\"isMountedRef.current = false\"]\n    end\n    \n    subgraph \"Usage in Component\"\n        H1[\"Check if Component is Mounted\"]\n        H1 --\u003e I1{isMountedRef.current ?}\n        I1 -- \"True\" --\u003e J1[\"Perform Mounted Actions\"]\n        I1 -- \"False\" --\u003e K1[\"Do Not Perform Actions\"]\n    end\n```\n\n## Contributing\n\nPlease read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.\n\n## Bugs and Sugestions\n\nReport bugs or do suggestions using the [issues](https://github.com/helderberto/use-is-mounted-ref/issues).\n\n## License\n\n[MIT License](LICENSE) © [helderberto](https://helderberto.com)\n\n\u003c!-- prettier-ignore-start --\u003e\n[version-badge]: https://img.shields.io/npm/v/use-is-mounted-ref.svg?style=flat-square\n[package]: https://www.npmjs.com/package/use-is-mounted-ref\n[downloads-badge]: https://img.shields.io/npm/dm/use-is-mounted-ref.svg?style=flat-square\n[npmtrends]: http://www.npmtrends.com/use-is-mounted-ref\n[license-badge]: https://img.shields.io/npm/l/use-is-mounted-ref.svg?style=flat-square\n[license]: https://github.com/helderberto/use-is-mounted-ref/blob/master/LICENSE\n[build]: https://github.com/helderberto/use-is-mounted-ref/actions\n[build-badge]: https://github.com/helderberto/use-is-mounted-ref/actions/workflows/ci.yml/badge.svg\n\u003c!-- prettier-ignore-end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelderberto%2Fuse-is-mounted-ref","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelderberto%2Fuse-is-mounted-ref","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelderberto%2Fuse-is-mounted-ref/lists"}