{"id":15656780,"url":"https://github.com/phryneas/rehackt","last_synced_at":"2025-04-05T23:05:10.468Z","repository":{"id":200682000,"uuid":"654217817","full_name":"phryneas/rehackt","owner":"phryneas","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-13T10:57:18.000Z","size":46,"stargazers_count":29,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T22:04:05.160Z","etag":null,"topics":["library"],"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/phryneas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"publiccode":null,"codemeta":null}},"created_at":"2023-06-15T16:25:04.000Z","updated_at":"2025-03-26T11:24:41.000Z","dependencies_parsed_at":"2024-10-23T05:08:33.407Z","dependency_job_id":"2f66b404-85b1-4cb3-821b-3f7b49f8120e","html_url":"https://github.com/phryneas/rehackt","commit_stats":null,"previous_names":["phryneas/rehackt"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Frehackt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Frehackt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Frehackt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phryneas%2Frehackt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phryneas","download_url":"https://codeload.github.com/phryneas/rehackt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411226,"owners_count":20934653,"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":["library"],"created_at":"2024-10-03T13:04:18.569Z","updated_at":"2025-04-05T23:05:10.439Z","avatar_url":"https://github.com/phryneas.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rehackt\r\n\r\n\u003e This package is fairly advanced and is only intended for library developers that want to maintain high interop with Next.js server actions.\r\n\r\nRehackt invisibly wraps `react` so that you're able to use shared imports with `react` in server-side Next.js code without throwing an error to your users.\r\n\r\n## Explainer\r\n\r\nAssume you have the following code in a Next.js codebase:\r\n\r\n```tsx\r\n\"use client\"\r\n\r\nimport { useFormState } from \"react-dom\"\r\nimport someAction from \"./action\";\r\n\r\nexport const ClientComp = () =\u003e {\r\n  const [data, action] = useFormState(someAction, \"Hello client\");\r\n\r\n  return \u003cform action={action}\u003e\r\n    \u003cp\u003e{data}\u003c/p\u003e\r\n    \u003cbutton type={\"submit\"}\u003eUpdate data\u003c/button\u003e\r\n  \u003c/form\u003e\r\n}\r\n```\r\n\r\n```tsx\r\n\"use server\"\r\n// action.ts\r\n\r\nimport {data} from \"./shared-code\";\r\n\r\nexport default async function someAction() {\r\n  return \"Hello \" + data.name;\r\n}\r\n```\r\n\r\n```tsx\r\n// shared-code.ts\r\nimport {useState} from \"react\";\r\n\r\nexport const data = {\r\n  useForm: \u003cT\u003e(val: T) =\u003e {\r\n      useState(val)\r\n  },\r\n  name: \"server\"\r\n}\r\n```\r\n\r\nWhile you're not intending to use `data.useForm` in your `action.ts` server-only file, you'll still receive the following error from Next.js' build process when trying to use this code:\r\n\r\n```shell\r\n./src/app/shared-code.ts\r\nReactServerComponentsError:\r\n\r\nYou're importing a component that needs useState. It only works in a Client Component but none of its parents are marked with \"use client\", so they're Server Components by default.\r\nLearn more: https://nextjs.org/docs/getting-started/react-essentials\r\n\r\n   ╭─[/src/app/shared-code.ts:1:1]\r\n 1 │ import {useState} from \"react\";\r\n   ·         ────────\r\n 2 │ \r\n 3 │ export const data = {\r\n 3 │   useForm: \u003cT\u003e(val: T) =\u003e {\r\n   ╰────\r\n\r\nMaybe one of these should be marked as a client entry with \"use client\":\r\n./src/app/shared-code.ts\r\n./src/app/action.ts\r\n```\r\n\r\nThis is because Next.js statically analyzes usage of `useState` to ensure it's not being utilized in server-only code.\r\n\r\nBy replacing the import from `react` to `rehackt`:\r\n\r\n```tsx\r\n// shared-code.ts\r\nimport {useState} from \"rehackt\";\r\n\r\nexport const data = {\r\n  useForm: \u003cT\u003e(val: T) =\u003e {\r\n      useState(val)\r\n  },\r\n  name: \"server\"\r\n}\r\n```\r\n\r\nYou'll no longer see this error.\r\n\r\n\u003e Keep in mind, this does not enable usage of `useState` in server-only code, this just removes the error described above.\r\n\r\n## Further Reading\r\n\r\nThe following is a list of reading resources that pertain to this package:\r\n\r\n- [My take on the current React \u0026 Server Components controversy - Lenz Weber-Tronic](https://phryneas.de/react-server-components-controversy)\r\n\r\n- [apollographql/apollo-client#10974](https://github.com/apollographql/apollo-client/issues/10974)\r\n\r\n- [TanStack/form#480](https://github.com/TanStack/form/issues/480#issuecomment-1793576645)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphryneas%2Frehackt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphryneas%2Frehackt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphryneas%2Frehackt/lists"}