{"id":13394343,"url":"https://github.com/developit/state-machine-component","last_synced_at":"2025-04-14T00:17:38.086Z","repository":{"id":66020092,"uuid":"118172222","full_name":"developit/state-machine-component","owner":"developit","description":"⚙️ State machine -powered components in 250 bytes","archived":false,"fork":false,"pushed_at":"2018-01-21T00:03:21.000Z","size":12,"stargazers_count":174,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T11:16:12.137Z","etag":null,"topics":["preact","preact-components","state-machine","state-management"],"latest_commit_sha":null,"homepage":"https://npm.im/state-machine-component","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/developit.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-01-19T20:01:39.000Z","updated_at":"2023-12-27T03:11:05.000Z","dependencies_parsed_at":"2023-03-10T23:29:28.220Z","dependency_job_id":null,"html_url":"https://github.com/developit/state-machine-component","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/developit%2Fstate-machine-component","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstate-machine-component/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstate-machine-component/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fstate-machine-component/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/state-machine-component/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799961,"owners_count":21163404,"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":["preact","preact-components","state-machine","state-management"],"created_at":"2024-07-30T17:01:16.543Z","updated_at":"2025-04-14T00:17:38.057Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/HasFoev.png\" width=\"300\" height=\"300\" alt=\"state-machine-component\"\u003e\n\u003c/p\u003e\n\u003ch1 align=\"center\"\u003estate-machine-component\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.org/package/state-machine-component\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/state-machine-component.svg?style=flat\" alt=\"npm\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nA tiny (~250 byte) utility to create state machine components using two pure functions.\n\n🔥 [**JSFiddle Demo**](https://jsfiddle.net/developit/x0td4bmy/)\n\n\n### Install\n\nThis project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.\n\n```sh\nnpm install --save state-machine-component\n```\n\nThen with a module bundler like [webpack](https://webpack.js.org) or [rollup](http://rollupjs.org), use as you would anything else:\n\n```js\nimport stateMachine from 'state-machine-component';\n```\n\nThe [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):\n\n```html\n\u003cscript src=\"//unpkg.com/state-machine-component/dist/state-machine-component.umd.js\"\u003e\u003c/script\u003e\n```\n\nThe library will install itself globally as `window.stateMachineComponent`.\n\n\n### Usage\n\nThe API is a single function that accepts 2 pure functions as arguments:\n\n```js\nstateMachineComponent(reduce, render)\n```\n\nThe first function, `reduce()`, takes in the current state and applies an `action` to it, similar to a reducer in Redux:\n\n```js\n// Reduce is a redux-style reducer\nfunction reduce(state, action) {\n\t// actions are like Redux Standard Actions:\n\tlet { type, data, props } = action\n\n\treturn { }  // just return the new state\n}\n```\n\nThe second function, `render()`, is a pure functional component that gets passed the current `state` instead of `props`, and a second argument `action()` - a function that creates a bound dispatcher for the given action type:\n\n```js\n// Render is a functional component with little twist\nfunction render(state, action) {\n\t// action() creates a dispatcher for an action type:\n\treturn \u003cbutton onClick={ action('TYPE') } /\u003e\n}\n```\n\n### Simple Example: Counter\n\n```js\n// Remember:\n//  `state` is the current state.\n//  `action` is a redux standard action.\nfunction reduce(state, action) {\n\tswitch (action.type) {\n\t\tcase '@@INIT': return { count: 0 }\n\t\tcase 'ADD': return { count: state.count+1 }\n\t}\n}\n\nfunction render(state, action) {\n\treturn (\n\t\t\u003cdiv class=\"counter\"\u003e\n\t\t\tCurrent count: {state.count}\n\t\t\t\u003cbutton onClick={action('ADD')}\u003eAdd 1\u003c/button\u003e\n\t\t\u003c/div\u003e\n\t)\n}\n\nstateMachineComponent(reduce, render)\n```\n\n\n### Full Example: To-Do List\n\n```js\nconst ToDos = stateMachineComponent(\n\t// (state, action)\n\t({ todos, text }, { type, data, props }) =\u003e {\n\t\tswitch (type) {\n\t\t\tcase '@@INIT':return { todos: props.todos || [], text: '' };\n\t\t\tcase 'ADD': return { todos: todos.concat(text), text: '' };\n\t\t\tcase 'TEXT': return { text: data.target.value };\n\t\t}\n\t},\n\t// state, action(type)\n\t({ todos, text }, action) =\u003e (\n\t\t\u003cdiv\u003e\n\t\t\t\u003ch2\u003eState Machine ToDos\u003c/h2\u003e\n\t\t\t\u003cul\u003e{todos.map( todo =\u003e \u003cli\u003e{todo}\u003c/li\u003e )}\u003c/ul\u003e\n\t\t\t\u003cform onSubmit={action('ADD')}\u003e\n\t\t\t\t\u003cinput value={text} onInput={action('TEXT')} /\u003e\n\t\t\t\u003c/form\u003e\n\t\t\u003c/div\u003e\n\t)\n);\n```\n\n\n### License\n\n[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com/)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fstate-machine-component","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fstate-machine-component","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fstate-machine-component/lists"}