{"id":25983690,"url":"https://github.com/foveluy/rectx","last_synced_at":"2025-03-05T10:31:53.640Z","repository":{"id":32148009,"uuid":"130335171","full_name":"Foveluy/rectx","owner":"Foveluy","description":" a light-weight state manager.","archived":false,"fork":false,"pushed_at":"2022-12-09T15:58:16.000Z","size":3105,"stargazers_count":176,"open_issues_count":20,"forks_count":15,"subscribers_count":9,"default_branch":"master","last_synced_at":"2023-10-20T19:53:08.235Z","etag":null,"topics":["javascript","react","redux"],"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/Foveluy.png","metadata":{"files":{"readme":"README.CN.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-04-20T08:43:33.000Z","updated_at":"2023-09-11T06:42:05.000Z","dependencies_parsed_at":"2023-01-14T20:37:23.083Z","dependency_job_id":null,"html_url":"https://github.com/Foveluy/rectx","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Foveluy%2Frectx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Foveluy%2Frectx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Foveluy%2Frectx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Foveluy%2Frectx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Foveluy","download_url":"https://codeload.github.com/Foveluy/rectx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242009967,"owners_count":20057176,"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":["javascript","react","redux"],"created_at":"2025-03-05T10:31:52.237Z","updated_at":"2025-03-05T10:31:53.111Z","avatar_url":"https://github.com/Foveluy.png","language":"JavaScript","readme":"# Rectx\n\n\u003e React + Context -\u003e Rectx, 轻量级状态管理库。\n\n## 安装\n\n```bash\nnpm install --save rectx\n```\n\n## 简单使用\n\n```js\nimport React from 'react'\nimport ReactDOM from 'react-dom'\nimport { Provider, Controller, Listen } from 'rectx'\n\nclass LikeController extends Controller {\n    state = {\n        isLike: false,\n        isMount: false\n    }\n\n    handleClick = () =\u003e {\n        this.setState({\n            isLike: !this.state.isLike\n        })\n    }\n}\n\nconst Like = () =\u003e (\n    \u003cListen\n        to={[LikeController]}\n        didMount={like =\u003e {\n            like.setState({ isMount: true })\n        }}\n    \u003e\n        {like =\u003e (\n            \u003cdiv\u003e\n                \u003cbutton onClick={() =\u003e like.handleClick()}\u003eClick me\u003c/button\u003e\n                \u003cdiv\u003e{like.state.isMount ? 'component being loaded' : 'component not loaded'}\u003c/div\u003e\n                \u003cdiv\u003e{like.state.isLike ? 'I love you' : 'I hate you'}\u003c/div\u003e\n            \u003c/div\u003e\n        )}\n    \u003c/Listen\u003e\n)\n\nReactDOM.render(\n    \u003cProvider\u003e\n        \u003cLike /\u003e\n    \u003c/Provider\u003e,\n    document.getElementById('root')\n)\n```\n\n现在我们已经完成了一个非常简单的例子。在这个例子中，我们定义了一个 LikeMachine 状态机，用于保存 Like 组件所需要的一切状态。\n\n## 意义何在？\n\n让我们认真的观察一下这段简单的代码，很快你就能发现其中的奥妙：\n\n```js\nclass LikeMachine extends Machine {\n    state = {\n        isLike: false\n    }\n\n    handleClick = () =\u003e {\n        this.setState({\n            isLike: !this.state.isLike\n        })\n    }\n}\n\nconst Like = () =\u003e (\n    \u003cListen to={[LikeMachine]}\u003e\n        {like =\u003e (\n            \u003cdiv\u003e\n                \u003cbutton onClick={() =\u003e like.handleClick()} /\u003e\n                \u003cdiv\u003e{like.state.isLike ? 'I love you' : 'I hate you'}\u003c/div\u003e\n            \u003c/div\u003e\n        )}\n    \u003c/Listen\u003e\n)\n```\n\n发现了吗？只要使用 `\u003cListen to={[LikeMachine]}/\u003e` 对组件进行包裹，那么这个组件就拥有了监听状态变化的能力。没错，这其实还是 PUB/SUB 模式的一种，但是相对于 Redux 我们更好理解。因为，我们依旧在使用 ``setState API `` 。\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoveluy%2Frectx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoveluy%2Frectx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoveluy%2Frectx/lists"}