{"id":18096357,"url":"https://github.com/bredele/react-states-machine","last_synced_at":"2025-04-13T10:05:06.568Z","repository":{"id":57345321,"uuid":"120542173","full_name":"bredele/react-states-machine","owner":"bredele","description":"React state management using finite state machine","archived":false,"fork":false,"pushed_at":"2018-07-09T15:07:55.000Z","size":65,"stargazers_count":28,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-26T10:03:25.291Z","etag":null,"topics":["flux","react","state","state-machine"],"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/bredele.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":"2018-02-07T00:40:43.000Z","updated_at":"2020-05-19T03:20:42.000Z","dependencies_parsed_at":"2022-09-14T04:31:32.942Z","dependency_job_id":null,"html_url":"https://github.com/bredele/react-states-machine","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/bredele%2Freact-states-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bredele%2Freact-states-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bredele%2Freact-states-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bredele%2Freact-states-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bredele","download_url":"https://codeload.github.com/bredele/react-states-machine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248695438,"owners_count":21146954,"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":["flux","react","state","state-machine"],"created_at":"2024-10-31T19:13:57.814Z","updated_at":"2025-04-13T10:05:06.541Z","avatar_url":"https://github.com/bredele.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React State Machine\n\n[![NPM](https://img.shields.io/npm/v/react-states-machine.svg?style=flat-square)](https://www.npmjs.com/package/react-states-machine)\n[![Downloads](https://img.shields.io/npm/dm/react-states-machine.svg?style=flat-square)](http://npm-stat.com/charts.html?package=react-states-machine)\n[![pledge](https://bredele.github.io/contributing-guide/community-pledge.svg)](https://github.com/bredele/contributing-guide/blob/master/community.md)\n\nInspired by [mood](https://github.com/bredele/mood) this module is using the well known [finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine) pattern to alleviate some of the issues that crop up in complex React applications by strictly separating the management of states from your components. This module stay true to the original intent behind React:\n  - Describe states as static components only (dynamic relationships within a component are expressed outside of the component itself)\n  - Describe the logic for handling changes/updates as simple functions (called transitions).\n  - Describe changes as plain objects to pass well defined and thought props.\n\n[Try online!](https://codesandbox.io/s/7jn717on4x)\n\n\nIn addition, this module makes easy to:\n  - develop stateless components (easier to understand and maintain)\n  - develop components in isolation (easier to reuse and scale)\n  - test components (dynamic relationships between components are tested separately)\n  - manage asynchronous changes (props can be resolved by promises)\n\n![react-states-machine](./diagram.png)\n\n## Usage\n\nA state is made of a component as well as a set of actions to be executed (called transitions). Those actions are called through transitions events and either update the current state or display a new state. A transition manages changes by passing props to the wanted state. Here's a simple example of navigation flow using react-states-machine:\n\n\n```js\nimport machine from 'react-states-machine'\n\nfunction NavigationFlow (attrs) {\n  return (\n    \u003csection\u003e\n      {\n        machine({\n          // welcome state\n          'welcome': [\n            props =\u003e \u003cbutton onClick={() =\u003e props.transition('click')}\u003eWelcome\u003c/button\u003e,\n            {\n              // click event transition from welcome state to next state with a new message prop\n              'click': [() =\u003e ({message: 'Hello you!'}), 'next']\n            }\n          ],\n          // next state\n          'next': [\n            props =\u003e {\n              return (\n                \u003cdiv\u003e\n                  \u003cbutton onClick={() =\u003e props.goto('welcome')}\u003eprevious\u003c/button\u003e\n                  {props.message}\n                  \u003cbutton onClick={() =\u003e props.transition('update')}\u003enext\u003c/button\u003e\n                \u003c/div\u003e\n              )\n            },\n            {\n              // update event update next state with new message prop after 1 second\n              'update': [() =\u003e new Promise(resolve =\u003e setTimeout(() =\u003e resolve({ message: 'This is awesome!' }), 1000))]\n            }\n          ]\n        }, attrs)\n      }\n    \u003c/section\u003e\n )\n}\n```\n\n## Getting started\n\nA state machine is an object describing your application/component states.\n\n```js\nmachine({\n  state: [\n    component,\n    transitions\n  ]\n})\n```\n\nA state is composed of a component as well as an optional object containing transitions to mutate this component. Here's an example that shows how to style an input when empty using a transition called `validity`:\n\n```js\nmachine({\n  'inputState': [\n    props =\u003e \u003cinput className={props.invalid ? 'invalid' : ''} onChange={e =\u003e props.transition('validity', e.target.value)}/\u003e,\n    {\n      'validity': [(prev, value) =\u003e {\n        return {\n          invalid: !value\n        }\n      }]\n    }\n  ]\n})\n```\n\nA transition is a function used to pass props to your component and update it. This function can return any types as well as promises (transition is resolved with the promise).\n\nA transition is also useful to describe the passage to an other state. Here's an example:\n\n```js\nmachine({\n  'formEmail': [\n    props =\u003e {\n      return (\n        \u003cdiv\u003e\n          \u003cinput type=\"email\" /\u003e\n          \u003cbutton onClick={() =\u003e props.transition('next')}\u003enext\u003c/button\u003e\n        \u003c/div\u003e\n      )\n    },\n    {\n      next: [() =\u003e ({name: 'John Doe'}), 'formPassword']\n    }\n  ],\n  'formPassword': [\n    props =\u003e {\n      return (\n        \u003cdiv\u003e\n          \u003ch2\u003eHello {props.name}\u003c/h2\u003e\n          \u003cinput type=\"password\" /\u003e\n          \u003cbutton\u003econnect\u003c/button\u003e\n        \u003c/div\u003e\n      )\n    }\n  ]\n})\n```\n\nBut you also can go to an other state without transition:\n\n```js\nmachine({\n  'formEmail': [\n    props =\u003e {\n      return (\n        \u003cdiv\u003e\n          \u003cinput type=\"email\" /\u003e\n          \u003cbutton onClick={() =\u003e props.goto('formPassword')}\u003enext\u003c/button\u003e\n        \u003c/div\u003e\n      )\n    }\n  ],\n  'formPassword': [\n    props =\u003e {\n      return (\n        \u003cdiv\u003e\n          \u003ch2\u003eHello {props.name}\u003c/h2\u003e\n          \u003cinput type=\"password\" /\u003e\n          \u003cbutton\u003econnect\u003c/button\u003e\n        \u003c/div\u003e\n      )\n    }\n  ]\n})\n```\n\nCheck out [our test suite](./test/react-states-machine.test.js) for more information.\n\n## Installation\n\n```shell\nnpm install react-states-machine --save\n```\n\n[![NPM](https://nodei.co/npm/react-states-machine.png)](https://nodei.co/npm/react-states-machine/)\n\n\n## Question\n\nFor questions and feedback please use our [twitter account](https://twitter.com/bredeleca). For support, bug reports and or feature requests please make sure to read our\n\u003ca href=\"https://github.com/bredele/contributing-guide/blob/master/community.md\" target=\"_blank\"\u003ecommunity guideline\u003c/a\u003e and use the issue list of this repo and make sure it's not present yet in our reporting checklist.\n\n## Contribution\n\nThis is an open source project and would not exist without its community. If you want to participate please make sure to read our \u003ca href=\"https://github.com/bredele/contributing-guide/blob/master/community.md\" target=\"_blank\"\u003eguideline\u003c/a\u003e before making a pull request. If you have any react-states-machine related project, component or other let everyone know in our wiki.\n\n\n## Licence\n\nThe MIT License (MIT)\n\nCopyright (c) 2016 Olivier Wietrich\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbredele%2Freact-states-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbredele%2Freact-states-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbredele%2Freact-states-machine/lists"}