{"id":28236996,"url":"https://github.com/bytebodger/use-constructor","last_synced_at":"2025-06-26T14:36:22.208Z","repository":{"id":57167234,"uuid":"345507641","full_name":"bytebodger/use-constructor","owner":"bytebodger","description":"A custom Hook that provides constructor-like functionality to functional React components","archived":false,"fork":false,"pushed_at":"2021-03-19T03:27:20.000Z","size":167,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-10T15:41:43.103Z","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/bytebodger.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-03-08T02:29:39.000Z","updated_at":"2021-08-27T13:51:07.000Z","dependencies_parsed_at":"2022-08-30T15:21:59.355Z","dependency_job_id":null,"html_url":"https://github.com/bytebodger/use-constructor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bytebodger/use-constructor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-constructor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-constructor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-constructor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-constructor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bytebodger","download_url":"https://codeload.github.com/bytebodger/use-constructor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytebodger%2Fuse-constructor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262085878,"owners_count":23256536,"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":"2025-05-19T00:17:14.167Z","updated_at":"2025-06-26T14:36:22.181Z","avatar_url":"https://github.com/bytebodger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-constructor\n\n`use-constructor` is a custom Hook that provides constructor-like functionality to functional React components. Functional components are not classes. As such, they have no true equivalent to a constructor.\n\n`useEffect()` is typically touted as the replacement for all lifecycle methods. But `useEffect()` always fires _after_ the component has rendered. Any code block that is called directly within the body of a functional component will fire every time that component is called (which can happen many successive times during React's reconciliation process).\n\nBy leveraging a simple ref variable, we can fire a code block before the component renders and ensure that it's only ever fired once for the life of the component.\n\n## Usage\n\nAfter installation, import the package as follows:\n\n```javascript\nimport { useConstructor } from '@toolz/use-constructor';\n```\n\n## Methods\n\n### useConstructor()\n\n```javascript\nconst API = {\n   arguments: {\n      callBack: {\n         required,\n         format: Function,\n      },\n   },\n   returns: void\n}\n```\n\n**Examples:**\n\n```javascript\nconst SomeComponent = () =\u003e {\n   useConstructor(() =\u003e {\n      /*\n         This code runs once, and only once, for the lifecycle of \n         this component.  This code also runs before the render \n         cycle.\n      */\n   });\n   \n   return \u003c\u003eSome JSX\u003c/\u003e\n}\n```\n\nSince this is a type of \"faux-constructor\" functionality, there is no magic that ensures the logic inside `useConstructor()` will run before _anything else_ happens in your functional component. So it's perfectly possible to process _other_ logic before you call `useConstructor()`. If you want it to act as a \"true\" constructor, you would need to ensure that it's called at the very top of the functional component.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fuse-constructor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytebodger%2Fuse-constructor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytebodger%2Fuse-constructor/lists"}