{"id":19453278,"url":"https://github.com/fuzetsu/staterino","last_synced_at":"2025-04-25T04:30:47.869Z","repository":{"id":42662960,"uuid":"274007011","full_name":"fuzetsu/staterino","owner":"fuzetsu","description":"Simple hook based state management.","archived":false,"fork":false,"pushed_at":"2023-01-07T06:06:35.000Z","size":1163,"stargazers_count":4,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T22:53:55.278Z","etag":null,"topics":["hooks","immutable","preact","react","state-management"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/fuzetsu.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":"2020-06-22T00:40:57.000Z","updated_at":"2023-07-31T10:40:16.000Z","dependencies_parsed_at":"2023-02-06T13:31:13.481Z","dependency_job_id":null,"html_url":"https://github.com/fuzetsu/staterino","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuzetsu%2Fstaterino","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuzetsu%2Fstaterino/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuzetsu%2Fstaterino/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuzetsu%2Fstaterino/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fuzetsu","download_url":"https://codeload.github.com/fuzetsu/staterino/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250754587,"owners_count":21481839,"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":["hooks","immutable","preact","react","state-management"],"created_at":"2024-11-10T17:03:20.697Z","updated_at":"2025-04-25T04:30:47.178Z","avatar_url":"https://github.com/fuzetsu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Staterino [![npm](https://img.shields.io/npm/v/staterino.svg)](https://www.npmjs.com/package/staterino) [![size](https://img.shields.io/bundlephobia/minzip/staterino)](https://unpkg.com/staterino@latest/dist/staterino.min.js)\n\nSimple hook based state management.\n\n## Example\n\n```jsx\nimport { render } from 'preact'\nimport * as hooks from 'preact/hooks'\nimport merge from 'mergerino'\nimport staterino from 'staterino'\n\nconst state = { count: 0 }\n\nconst useStore = staterino({ merge, hooks, state })\n\nconst { set, get, subscribe } = useStore\nconst increment = () =\u003e set({ count: x =\u003e x + 1 })\nconst decrement = () =\u003e set({ count: x =\u003e x - 1 })\n\n// reset count when it reaches 11\nsubscribe(\n  s =\u003e s.count,\n  count =\u003e {\n    if (Math.abs(count) \u003e 10) set({ count: 0 })\n  }\n)\n\nconst App = () =\u003e {\n  const count = useStore(s =\u003e s.count)\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eCount is {count}\u003c/p\u003e\n      \u003cbutton onClick={increment}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={decrement}\u003e-\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n\nrender(\u003cApp /\u003e, document.getElementById('app'))\n```\n\n[Code sandbox](https://codesandbox.io/s/staterino-example-f0de8?file=/src/index.js)\n\n## Usage\n\nStaterino exports a single function: `staterino`.\n\nThis function creates a staterino data store, it expects a single object of the following shape as its only parameter:\n\n```js\nconst useStore = staterino({\n  // initial state object\n  state,\n  // reducer function that combines current state with a patch\n  merge,\n  // staterino relies on these two hooks to function\n  hooks: { useLayoutEffect, useReducer }\n})\n```\n\nYou can create as many data stores as you like, each holding their own isolated state.\n\n`useStore` accepts one parameter, a state selector.\n\nA state selector can be either a function:\n\n```js\nconst count = useStore(state =\u003e state.counter.count)\n```\n\na string:\n\n```js\nconst count = useStore('counter.count')\n```\n\nor an array of strings/functions:\n\n```js\nconst [count, age] = useStore(['counter.count', state =\u003e state.age])\n```\n\nIf you pass an array the hook will return an array as well with the state slices in the correct order.\n\nIf no arguments are passed `useStore()` will return the whole state object:\n\n```js\nconst state = useStore()\n```\n\n`useStore` is the hook itself, but it contains 3 essential functions:\n\n```js\nconst {\n  // sends a patch to be merged with the current state\n  set,\n  // getter for current state\n  get,\n  // allows you to react to state changes outside of components\n  subscribe\n} = useStore\n```\n\n`subscribe` takes two parameters, a state selector or array of state selectors, and a callback for when the subscribed portion of state changes:\n\n```js\n// the subscribe call returns a function used to unsubscribe\nconst unSub = subscribe(\n  // the state selector\n  ['counter.count', state =\u003e state.age],\n  // the callback function that triggers when state changes\n  (count, age) =\u003e {\n    console.log(count, age)\n    // optional cleanup function, similar to useEffect\n    return () =\u003e console.log('cleaning up', count, age)\n  }\n)\n```\n\nThe selector parameter works under the same rules as the one passed to `useStore`, it can be a string a function or an array with a mix of the two.\n\nIf you just want to subscribe to any change to the state overall you can just pass a single parameter as a shorthand:\n\n```js\nsubscribe(state =\u003e {\n  // do something whenever state changes\n})\n```\n\n## Credits\n\nInspired by [zustand](https://github.com/react-spring/zustand) ❤️\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuzetsu%2Fstaterino","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffuzetsu%2Fstaterino","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuzetsu%2Fstaterino/lists"}