{"id":13450940,"url":"https://github.com/renansoares/useWaitForElements","last_synced_at":"2025-03-23T16:32:37.724Z","repository":{"id":53728427,"uuid":"341545935","full_name":"renansoares/useWaitForElements","owner":"renansoares","description":"A React Hook to check when elements are rendered.","archived":false,"fork":false,"pushed_at":"2021-03-17T15:18:58.000Z","size":115,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-08T23:54:07.862Z","etag":null,"topics":["hooks","mutationobserver","react"],"latest_commit_sha":null,"homepage":"https://github.com/renansoares/useWaitForElements","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/renansoares.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}},"created_at":"2021-02-23T12:22:18.000Z","updated_at":"2023-08-04T20:43:28.000Z","dependencies_parsed_at":"2022-08-30T07:41:18.688Z","dependency_job_id":null,"html_url":"https://github.com/renansoares/useWaitForElements","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/renansoares%2FuseWaitForElements","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renansoares%2FuseWaitForElements/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renansoares%2FuseWaitForElements/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/renansoares%2FuseWaitForElements/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/renansoares","download_url":"https://codeload.github.com/renansoares/useWaitForElements/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245131131,"owners_count":20565775,"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","mutationobserver","react"],"created_at":"2024-07-31T07:00:40.609Z","updated_at":"2025-03-23T16:32:37.357Z","avatar_url":"https://github.com/renansoares.png","language":"TypeScript","readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003euseWaitForElements ⏳\u003c/h1\u003e\n\n\u003cp\u003e\nA React hook to be used to know when elements are rendered built with \n\u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver\"\u003e\n\u003cstrong\u003eMutationObserver\u003c/a\u003e\u003c/strong\u003e.\n\u003c/p\u003e\n\n\u003c/div\u003e\n\n## When do I need it?\n\nSometimes, you need to know when elements are currently rendered. One of the ways to do it is using MutationObserver. Using MutationObserver, this hook allows to know when a list of elements are rendered. \n\nReact provides a [`solution`](https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node) when you want to know if a component is rendered that might be better than using this hook in some cases.\n\n**This hook only tracks until all the elements are rendered. It stops observing after it, so if you are observing elements that can change state after the first render. I would recommend forking the repository and changing it.**\n\n## Installation\n\n```shell\nnpm install --save usewaitforelements\n```\n\n## Usage\n\nYou need to pass to the hook an object with an identifier and a [`selector`](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors) for each element.\n\nHook will return an object with the id, and a boolean indicating the presence for each element.\n\n## Example\n\n```jsx\nimport React, { useState } from 'react'\nimport ReactDOM from 'react-dom'\nimport useWaitForElements from 'usewaitforelements'\n\nfunction Component(...props) {\n  const [shouldRender, setShouldRender] = useState(false);\n\n  // Pass an object of ids and selectors to the hook\n  const result = useWaitForElements({ element: '#elementId' });\n  // Initial result will be { element: false }\n\n  useEffect(() =\u003e {\n      // Element will be rendered in 2 seconds\n      setTimeout(() =\u003e setShouldRender(true), 2000);\n      // After element is rendered, result will be { element: true }\n  }, [setShouldRender]);\n\n  return (\u003cdiv\u003e\n            {shouldRender \u0026\u0026 \u003cp id=\"elementId\"\u003eElement\u003c/p\u003e}\n            {!result.element \u0026\u0026 \u003cp\u003eElement is not rendered\u003c/p\u003e}\n            {result.element \u0026\u0026 \u003cp\u003eElement is rendered\u003c/p\u003e}\n         \u003c/div\u003e)\n}\n```\n\n### 🐛 Bugs\n\nPlease open an issue if you find a bug.\n\n### 💻 Pull Requests\n\nPull requests are welcome! Feel free to submit a PR if you feel like it.\n\n## LICENSE\nMIT","funding_links":[],"categories":["Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenansoares%2FuseWaitForElements","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frenansoares%2FuseWaitForElements","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenansoares%2FuseWaitForElements/lists"}