{"id":19332467,"url":"https://github.com/yuchi/react-use-refs","last_synced_at":"2026-03-08T09:31:30.093Z","repository":{"id":50703348,"uuid":"421012323","full_name":"yuchi/react-use-refs","owner":"yuchi","description":"Hook to create multiple refs in a single call","archived":false,"fork":false,"pushed_at":"2021-11-22T12:41:40.000Z","size":11,"stargazers_count":213,"open_issues_count":1,"forks_count":4,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-02T05:12:41.883Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuchi.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-10-25T12:21:54.000Z","updated_at":"2025-03-30T00:48:36.000Z","dependencies_parsed_at":"2022-09-06T02:10:29.034Z","dependency_job_id":null,"html_url":"https://github.com/yuchi/react-use-refs","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/yuchi%2Freact-use-refs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Freact-use-refs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Freact-use-refs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuchi%2Freact-use-refs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuchi","download_url":"https://codeload.github.com/yuchi/react-use-refs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250340639,"owners_count":21414602,"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-10T02:45:56.553Z","updated_at":"2026-03-08T09:31:30.055Z","avatar_url":"https://github.com/yuchi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useRefs ♻️\n\n\u003e React hook to create multiple refs in a single call.\n\n## Usage\n\nInstead of calling `useRef` many times, you just call `useRefs` and destructure **as many refs as you want!**\n\n```js\nimport useRefs from \"react-use-refs\";\n\nconst [someRef, anotherRef] = useRefs();\n```\n\nThe first and only argument is the `initialValue` of the refs. There’s no way to specify a different value for each ref. Use a list of `React.useRef`s for that!\n\n## TypeScript support\n\nThis library supports TypeScript and should work exactly as `React.useRef` does.\n\n```tsx\nconst [cardRef, cardBodyRef] = useRefs\u003cHTMLDivElement\u003e();\n\nreturn \u003cdiv ref={cardRef}\u003e ... \u003c/div\u003e;\n```\n\nIf you want to have multiple ref types, then you can pass a tuple and have it spread onto the created refs:\n\n```tsx\nconst [cardRef, inputRef] = useRefs\u003c[HTMLDivElement, HTMLInputElement]\u003e(null);\n```\n\n\u003e ⚠️ Passing `null` as the `initialValue` is required for tuples!\n\n## Frequently Asked Questions\n\n#### 🧙‍♀️ Is this black magic?\n\nNo. Keep reading.\n\n#### 🤔 So how does this work?!\n\nThis hook returns an iterable object, something you can use in a `for (of)` statement, with `Array.from()` or (and this is the neat part) with the Array Destructuring syntax (the one you use for `state`/`setState` pairs for example.)\n\nAlso, if you have a look at the code the returned value from the iterator has always the `done` flag set to `false`. This is an infinite loop disguised as an iterator! But since calling next during array destructuring happens a finite number of times, hence we do not hit typical infinte loop behaviour (aka frozen page).\n\n#### 📜 Does this break the Rules of Hooks?\n\n**Short answer: no.** Real answer: it’s up to you. Actual real answer follows.\n\nThe [_Rules of Hooks_ section of React’s official documentation cite](https://reactjs.org/docs/hooks-rules.html): \u003cq\u003eDon’t call Hooks inside loops, conditions, or nested functions.\u003c/q\u003e\n\nAs you can see in the source code we are definitely breaking this rule by calling `useRef` inside the `next()` method of the Iterable.\n\nBut we need to understand the the Rules of Hooks exist for a reason, and that is to have statically stable invocation of primitive hooks between re-renders.\n\nSince we explicitly encourage the use of Array Destructuring, the dynamic part is made “static” by hard-coding it in your own source code. We therefore **do not** break the rules of hooks.\n\n#### 😈 But wait I can manually call `.next()` conditionally!\n\nYeah, you can do some bad stuff with the returned iterator, but it’s not that different from having, for example, the following code:\n\n```js\nconst iSwearIAmNotUseRef = React.useRef();\n```\n\nThe only issue is that using the returned iterator doesn’t throw a ESLint warning at you as the above code would.\n\n#### 😕 Ok, but why did you do it?\n\nBecause I could. And because [@drcmda](https://github.com/drcmda) said he would use it and would love it. ❤️ Spread love and not some silly questions about what people do in their free time.\n\n## Credits\n\n- Thanks to [@drcmda](https://github.com/drcmda) for stating his need for such an API.\n- Thanks to [@Andarist](https://github.com/Andarist) for the initial TypeScript types definition.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuchi%2Freact-use-refs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuchi%2Freact-use-refs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuchi%2Freact-use-refs/lists"}