{"id":13452547,"url":"https://github.com/mweststrate/use-st8","last_synced_at":"2025-03-31T10:08:22.346Z","repository":{"id":33950896,"uuid":"163877478","full_name":"mweststrate/use-st8","owner":"mweststrate","description":"Single-function alternative for React.useState","archived":false,"fork":false,"pushed_at":"2023-07-11T21:39:57.000Z","size":825,"stargazers_count":235,"open_issues_count":6,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T12:28:40.485Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/mweststrate.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-01-02T18:48:21.000Z","updated_at":"2025-02-11T01:16:13.000Z","dependencies_parsed_at":"2024-01-06T08:00:43.874Z","dependency_job_id":"10e13af6-a5f0-4b5a-8566-a2b9717bd81f","html_url":"https://github.com/mweststrate/use-st8","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/mweststrate%2Fuse-st8","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mweststrate%2Fuse-st8/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mweststrate%2Fuse-st8/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mweststrate%2Fuse-st8/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mweststrate","download_url":"https://codeload.github.com/mweststrate/use-st8/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246450471,"owners_count":20779408,"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-07-31T07:01:27.171Z","updated_at":"2025-03-31T10:08:22.320Z","avatar_url":"https://github.com/mweststrate.png","language":"TypeScript","funding_links":["https://www.paypal.me/michelweststrate","https://www.buymeacoffee.com/mweststrate"],"categories":["TypeScript"],"sub_categories":[],"readme":"# use-st8\n\nSingle-function alternative for React.useState\n\n[![npm](https://img.shields.io/npm/v/use-st8.svg)](https://www.npmjs.com/package/use-st8) [![size](http://img.badgesize.io/https://unpkg.com/use-st8@0.0.1/lib/index.mjs?compression=gzip)](http://img.badgesize.io/https://unpkg.com/use-st8@0.0.1/lib/index.mjs) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)\n\n# Installation\n\n`npm add use-st8`\n\n# Quick example\n\n`usest8` is a single function alternative for the `useState` hook (typically: `const [currentValue, updater] = useState(initial)`), that combines the current value constant and updater function into a single function.\n\n```javascript\nimport * as React from \"react\";\nimport { render } from \"react-dom\";\nimport { useSt8 } from \"use-st8\"; // (or) import useSt8 from 'use-st8';\n\nfunction App() {\n  const count = useSt8(0);\n\n  return (\n    \u003cdiv className=\"App\"\u003e\n      \u003ch1\u003eHello CodeSandbox\u003c/h1\u003e\n      \u003ch2\u003e{count()}\u003c/h2\u003e\n      \u003cbutton onClick={() =\u003e count(c =\u003e c - 1)}\u003e-\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e count(c =\u003e c + 1)}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e count(0)}\u003eReset\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n\nconst rootElement = document.getElementById(\"root\");\nrender(\u003cApp /\u003e, rootElement);\n```\n\nOpen the demo in [code-sandbox](https://codesandbox.io/s/q9q7yrxjkj) to see it in action\n\n# API\n\n```javascript\n// create a new local state value in a React function component\nconst count = useSt8(0)\n\n// same, but with initializer function\nconst count = useSt8(() =\u003e 0)\n\n// get the current state\ncount() \n\n// change the state with to the given value\ncount(0)\n\n// update the state using an updater function, that receives the current state and returns the next one\ncount(c =\u003e c + 1)\n```\n\n`useSt8` has the same call-signature as the [React `useState`](https://reactjs.org/docs/hooks-state.html) hook. \nExcept, instead of returning a tuple with the current value and a setter, it returns a single function.\nThe function returned can be called in two different ways:\n* With zero arguments. In that case the current state is returned.\n* With one argument. In that case the current state is updated with the given value, or using an updater function, just like the normal `useState` [update function](https://reactjs.org/docs/hooks-reference.html#functional-updates).\n\nThat's all.\n\n# Benefits\n\n* No array destructurings needed, which polute your closures with name pairs, like `const [count, setCount] = useState(0)`. Instead, `const count = useSt8(0)` just reads nicer. And it saves some characters. Super important. All IMHO 😉.\n* 🚀 Doesn't rely on array destructurings, which are potentially slow as they use the iterator protocol ([background](https://docs.google.com/document/d/1hWb-lQW4NSG9yRpyyiAA_9Ktytd5lypLnVLhPX9vamE/edit)). Note that you probably won't notice this in practice, so this is more of a fun fact than an argument to use this. \n\n# Example\n\nWith `useState` ([offical example](https://reactjs.org/docs/hooks-reference.html#functional-updates)):\n\n```javascript\nimport { useState } from \"react\"\n\nfunction Counter({initialCount}) {\n  const [count, setCount] = useState(initialCount);\n  return (\n    \u003c\u003e\n      Count: {count}\n      \u003cbutton onClick={() =\u003e setCount(0)}\u003eReset\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e setCount(prevCount =\u003e prevCount + 1)}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e setCount(prevCount =\u003e prevCount - 1)}\u003e-\u003c/button\u003e\n    \u003c/\u003e\n  );\n}\n```\n\nWith `useSt8`:\n\n```javascript\nimport { useSt8 } from \"use-st8\"\n\nfunction Counter({initialCount}) {\n  const count = useSt8(initialCount);\n  return (\n    \u003c\u003e\n      Count: {count()}\n      \u003cbutton onClick={() =\u003e count(0)}\u003eReset\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e count(prevCount =\u003e prevCount + 1)}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e count(prevCount =\u003e prevCount - 1)}\u003e-\u003c/button\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n_[sarcasm]Which saves a whooping 21 characters. Now go forth and refactoring all the things![/sarcasm]_\n\n# The name\n\n_useSt8_ is a shorter form of _useState_, which has _8_ characters. Also, the pronounciation is pretty similar to \"useState\".\n\nIf you prefer to use with a different name, the `useSt8` named export is set as the default export as well.\n\n```javascript\nimport useSt8 from 'use-st8';\nimport useCustomNameSt8 from 'use-st8';\n```\n\n# Cool?\n\nDo you think this cool and useful? Consider buying me a coffee!\u003cbr/\u003e\u003ca href=\"https://www.buymeacoffee.com/mweststrate\" target=\"_blank\"\u003e\u003cimg src=\"https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: auto !important;width: auto !important;\" \u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmweststrate%2Fuse-st8","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmweststrate%2Fuse-st8","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmweststrate%2Fuse-st8/lists"}