{"id":18777745,"url":"https://github.com/arn4v/use-children","last_synced_at":"2025-09-01T16:33:37.980Z","repository":{"id":57387957,"uuid":"385375531","full_name":"arn4v/use-children","owner":"arn4v","description":"Filter React children for a specific component","archived":false,"fork":false,"pushed_at":"2021-07-13T14:37:33.000Z","size":118,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-29T10:14:13.474Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/use-children","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/arn4v.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":"2021-07-12T20:30:40.000Z","updated_at":"2021-07-29T16:19:22.000Z","dependencies_parsed_at":"2022-09-02T01:11:59.271Z","dependency_job_id":null,"html_url":"https://github.com/arn4v/use-children","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/arn4v%2Fuse-children","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arn4v%2Fuse-children/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arn4v%2Fuse-children/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arn4v%2Fuse-children/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arn4v","download_url":"https://codeload.github.com/arn4v/use-children/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239690076,"owners_count":19681034,"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-07T20:13:41.813Z","updated_at":"2025-02-19T16:17:19.892Z","avatar_url":"https://github.com/arn4v.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-children\n\nFilter React children for specific component\n\n## Install\n\n**NPM**:\n\n```shell\nnpm install --save use-children\n```\n\n**Yarn**:\n\n```shell\nyarn add use-children\n```\n\n## Usage\n\nRead the [Why?](#why) section for a more detailed explanation.\n\n```jsx\nimport useChildren from \"useChildren\";\n\nconst ModalContent = () =\u003e {\n  return \u003c\u003e{/** Some JSX */}\u003c/\u003e;\n};\n\nconst ModalOverlay = () =\u003e {\n  return \u003c\u003e{/** Some JSX */}\u003c/\u003e;\n};\n\nconst ModalRoot = ({ children, isOpen }) =\u003e {\n  // withOverlay is an array of all ModalOverlay components\n  const [withOverlay] = useChildren(children, ModalOverlay);\n  // withContent is an array of all MdalContent components\n  const [withContent] = useChildren(children, ModalContent);\n\n  return (\n    \u003cdiv className=\"modal-root\"\u003e\n      {isOpen ? (\n        \u003c\u003e\n          {withOverlay}\n          {withContent}\n        \u003c/\u003e\n      ) : null}\n    \u003c/div\u003e\n  );\n};\n```\n\n## Why?\n\nImagine you're building a Modal component which consists of three components:\n\n1. ModalRoot - Portal component\n2. ModalOverlay - Overlay component\n3. ModalContent - The primary content container component\n\nwhere the intended usage is the following:\n\n```jsx\nimport { useState } from \"react\";\nimport { ModalRoot, ModalOverlay, ModalContent } from \"../modal\";\n\nconst MyModal = () =\u003e {\n  const [open, setOpen] = useState(false);\n\n  const openModal = () =\u003e setOpen(true);\n  const closeModal = () =\u003e setOpen(false);\n\n  return (\n    \u003cModalRoot isOpen={open}\u003e\n      \u003cModalOverlay /\u003e\n      \u003cModalContent /\u003e\n    \u003c/ModalRoot\u003e\n  );\n};\n```\n\nHowever, since ModalRoot does not force the user to pass in only ModalOverlay and ModalContent. The user can also do the following:\n\n```jsx\nimport { useState } from \"react\";\nimport { ModalRoot, ModalOverlay, ModalContent } from \"../modal\";\n\nconst MyModal = () =\u003e {\n  const [open, setOpen] = useState(false);\n\n  const openModal = () =\u003e setOpen(true);\n  const closeModal = () =\u003e setOpen(false);\n\n  return (\n    \u003cModalRoot isOpen={open}\u003e\n      \u003cdiv\u003eSome content\u003c/div\u003e\n    \u003c/ModalRoot\u003e\n  );\n};\n```\n\nWith `use-children` you can filter for just `ModalContent` and `ModalOverlay`\n\n## Credits\n\n- [Geist-UI](https://github.com/geist-org/react) for the [pickChild](https://github.com/geist-org/react/blob/master/components/utils/collections.ts#L24) function\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farn4v%2Fuse-children","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farn4v%2Fuse-children","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farn4v%2Fuse-children/lists"}