{"id":22812119,"url":"https://github.com/luncheon/preact-shared-state-hook","last_synced_at":"2025-03-30T21:30:19.641Z","repository":{"id":57329606,"uuid":"298490394","full_name":"luncheon/preact-shared-state-hook","owner":"luncheon","description":"Most straightforward way to create shareable state hooks for Preact.","archived":false,"fork":false,"pushed_at":"2022-09-07T23:13:06.000Z","size":231,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T05:33:32.372Z","etag":null,"topics":["hook","preact","state"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"wtfpl","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luncheon.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":"2020-09-25T06:43:46.000Z","updated_at":"2022-10-25T13:51:36.000Z","dependencies_parsed_at":"2022-09-14T18:40:25.706Z","dependency_job_id":null,"html_url":"https://github.com/luncheon/preact-shared-state-hook","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fpreact-shared-state-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fpreact-shared-state-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fpreact-shared-state-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luncheon%2Fpreact-shared-state-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luncheon","download_url":"https://codeload.github.com/luncheon/preact-shared-state-hook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246258863,"owners_count":20748573,"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":["hook","preact","state"],"created_at":"2024-12-12T12:09:59.350Z","updated_at":"2025-03-30T21:30:19.363Z","avatar_url":"https://github.com/luncheon.png","language":"TypeScript","readme":"\u003e There is an official global state library [@preact/signals](https://preactjs.com/guide/v10/signals), that is much better!\n\n# preact-shared-state-hook\n\n![Types: included](https://badgen.net/npm/types/preact-shared-state-hook) ![License: WTFPL](https://badgen.net/npm/license/preact-shared-state-hook)\n\nMost straightforward way to create shareable state hooks for [Preact](https://preactjs.com/).  \nIt's only 30 lines of [source code](https://github.com/luncheon/preact-shared-state-hook/blob/main/index.js).\n\n## Installation\n\n```bash\n$ npm i preact-shared-state-hook\n```\n\n## Usage\n\nCreate state hooks with `createSharedState()` and use them in components.  \nThe state value can be shared by multiple components.\n\n```jsx\nimport { h, render } from \"preact\"\nimport { memo } from \"preact/compat\"\nimport { createSharedState } from \"preact-shared-state-hook\"\n\n// create a state hook\nconst [useCount, setCount] = createSharedState(0)\n\nconst Counter = memo(() =\u003e {\n  // use the state value\n  const count = useCount()\n  return \u003cbutton type=\"button\" onClick={() =\u003e setCount(count + 1)}\u003e{count}\u003c/button\u003e\n})\n\nrender(\u003cCounter /\u003e, document.body.appendChild(document.createElement('main')))\n```\n\nCreate computation hooks with `createSharedSelector()` and use them in components.  \nThe computed value can be shared by multiple components.\n\n```jsx\nimport { h, render, Fragment } from \"preact\"\nimport { memo } from \"preact/compat\"\nimport { createSharedState, createSharedSelector } from \"preact-shared-state-hook\"\n\nconst [useCount1, setCount1] = createSharedState(0)\nconst [useCount2, setCount2] = createSharedState(0)\n\n// create a computation hook\nconst useProduct = createSharedSelector([useCount1, useCount2], (count1, count2) =\u003e count1 * count2)\n\nconst Counter = memo(() =\u003e {\n  const count1 = useCount1()\n  const count2 = useCount2()\n  return (\n    \u003cFragment\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e setCount1(count1 + 1)}\u003e{count1}\u003c/button\u003e\n      \u003cbutton type=\"button\" onClick={() =\u003e setCount2(count2 + 1)}\u003e{count2}\u003c/button\u003e\n    \u003c/Fragment\u003e\n  )\n})\n\nconst Product = memo(() =\u003e {\n  // use the computed value\n  const product = useProduct()\n  return \u003cdiv\u003e{product}\u003c/div\u003e\n})\n\nconst App = memo(() =\u003e {\n  return (\n    \u003cFragment\u003e\n      \u003cCounter /\u003e\n      \u003cProduct /\u003e\n    \u003c/Fragment\u003e\n  )\n})\n\nrender(\u003cApp /\u003e, document.body.appendChild(document.createElement('main')))\n```\n\nActions can be predefined.\n\n```jsx\nimport { h, render } from \"preact\"\nimport { memo } from \"preact/compat\"\nimport { createSharedState } from \"preact-shared-state-hook\"\n\nconst [useCount, setCount] = createSharedState(0)\n\n// action\nconst increment = () =\u003e setCount(count =\u003e count + 1)\n\nconst Counter = memo(() =\u003e {\n  const count = useCount()\n  return \u003cbutton type=\"button\" onClick={increment}\u003e{count}\u003c/button\u003e\n})\n\nrender(\u003cCounter /\u003e, document.body.appendChild(document.createElement('main')))\n```\n\n[CodeSandbox](https://codesandbox.io/s/preact-shared-state-hook-example-i6qi2?file=/index.tsx)\n\n## API\n\n### createSharedState\n\n```ts\nconst [useSharedState, setSharedState] = createSharedState(initialState)\n\nconst state = useSharedState()\n\nsetSharedState(nextState)\nsetSharedState(state =\u003e nextState)\n```\n\nCreates a hook to share a state.\n\nThe first returned value `useSharedState()` returns the current state value.  \n`useSharedState()` must be called in functional components.\n\nThe second returned value `setSharedState()` updates the state and re-renders the components that have called `useSharedState()`.  \n`setSharedState()` accepts a new state value or a function that computes a new state value from an old state value.  \n`setSharedState()` can be called anywhere.\n\n### createSharedSelector\n\n```ts\nconst useSharedSelector = createSharedSelector([useSharedState1, useSharedState2, ...], (state1, state2, ...) =\u003e value)\n\nconst value = useSharedSelector()\n```\n\nCreates a hook to compute a value from some shared states.  \n`useSharedSelector()` must be called in functional components.\n\n## License\n\n[WTFPL](http://www.wtfpl.net)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluncheon%2Fpreact-shared-state-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluncheon%2Fpreact-shared-state-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluncheon%2Fpreact-shared-state-hook/lists"}