{"id":13671352,"url":"https://github.com/nanxiaobei/retalk","last_synced_at":"2025-04-06T12:09:17.693Z","repository":{"id":32548353,"uuid":"136696408","full_name":"nanxiaobei/retalk","owner":"nanxiaobei","description":"🐤 The Simplest Redux","archived":false,"fork":false,"pushed_at":"2024-09-27T18:23:03.000Z","size":1666,"stargazers_count":188,"open_issues_count":0,"forks_count":17,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-27T16:14:24.894Z","etag":null,"topics":["actions","flux","javascript","react","reducers","redux","retalk","state"],"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/nanxiaobei.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-09T06:18:31.000Z","updated_at":"2025-01-17T10:17:41.000Z","dependencies_parsed_at":"2023-01-14T21:33:47.385Z","dependency_job_id":"336098b4-3adc-4fa9-b887-0de8d5db3247","html_url":"https://github.com/nanxiaobei/retalk","commit_stats":{"total_commits":325,"total_committers":2,"mean_commits":162.5,"dds":0.07999999999999996,"last_synced_commit":"da56922e22a11e1a184eb4817cb416c65372cf15"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanxiaobei%2Fretalk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanxiaobei%2Fretalk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanxiaobei%2Fretalk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanxiaobei%2Fretalk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanxiaobei","download_url":"https://codeload.github.com/nanxiaobei/retalk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478323,"owners_count":20945266,"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":["actions","flux","javascript","react","reducers","redux","retalk","state"],"created_at":"2024-08-02T09:01:07.160Z","updated_at":"2025-04-06T12:09:17.652Z","avatar_url":"https://github.com/nanxiaobei.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\nLink in bio to **widgets**,\nyour online **home screen**. ➫ [🔗 kee.so](https://kee.so/)\n\n\u003c/div\u003e\n\n---\n\n\u003cdiv align=\"center\"\u003e\n\u003cimg src=\"./logo.png\" width=\"228\" alt=\"Retalk\"\u003e\n\nThe Simplest Redux\n\n[![Travis](https://img.shields.io/travis/nanxiaobei/retalk.svg?style=flat-square)](https://travis-ci.org/nanxiaobei/retalk)\n[![npm version](https://img.shields.io/npm/v/retalk.svg?style=flat-square)](https://www.npmjs.com/package/retalk)\n[![npm bundle size](https://img.shields.io/bundlephobia/minzip/retalk?style=flat-square)](https://bundlephobia.com/result?p=retalk)\n[![npm downloads](https://img.shields.io/npm/dt/retalk.svg?style=flat-square)](http://www.npmtrends.com/retalk)\n[![license](https://img.shields.io/github/license/nanxiaobei/retalk.svg?style=flat-square)](https://github.com/nanxiaobei/retalk/blob/master/LICENSE)\n\nEnglish · [简体中文](./README.zh-CN.md)\n\n\u003c/div\u003e\n\n---\n\n## Features\n\n- **Simplest** - Same syntax as a class component\n- **Only 2 API** - `setStore()` and `withStore()`\n- **Async model** - Code splitting support for models\n- **Auto loading** - Auto loading state for async actions\n\n## Install\n\n```sh\nyarn add retalk\n\n# npm i retalk\n```\n\n## Usage\n\nModel syntax is like a React class component, just without lifecycle methods.\n\n```jsx\nimport { Provider, setStore, withStore } from 'retalk';\n\n// Setup model\nclass CounterModel {\n  state = {\n    count: 0,\n  };\n  add() {\n    const { count } = this.state; // get own state\n    this.setState({ count: ++count }); // set own state\n    this.addAsync(); // run own action\n\n    // this.models.someModel.state        -\u003e get another model's state\n    // this.models.someModel.someAction() -\u003e run another model's action\n  }\n  async addAsync() {\n    await new Promise((resolve) =\u003e setTimeout(resolve, 1000));\n    const { count } = this.state;\n    this.setState({ count: ++count });\n  }\n}\n\n// Use in components\nconst Counter = withStore({\n  counter: ['count', 'add', 'addAsync'],\n})((props) =\u003e {\n  const { count, add, addAsync } = props; // addAsync.loading can be use\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{count}\u003c/p\u003e\n      \u003cbutton onClick={add}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={addAsync}\u003e+ ⏳{addAsync.loading \u0026\u0026 '...'}\u003c/button\u003e\n    \u003c/div\u003e\n  );\n});\n\n// Setup store\nconst store = setStore({ counter: CounterModel });\n\nconst App = () =\u003e (\n  \u003cProvider store={store}\u003e\n    \u003cCounter /\u003e\n  \u003c/Provider\u003e\n);\n```\n\n## Demo\n\n[![Edit retalk](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/retalk-5l9mqnzvx?fontsize=14\u0026file=/src/Counter/Index.jsx)\n\n## API\n\n### 1. setStore()\n\n`setStore(models, middleware)`\n\n```js\nconst store = setStore(\n  {\n    home: HomeModel,\n    counter: CounterModel,\n  },\n  [logger, crashReporter]\n);\n```\n\nPass `models` and `middleware`(both are optional), Set up the one and only store.\n\nIn `development` mode, [Redux DevTools](https://github.com/zalmoxisus/redux-devtools-extension) will be enabled by default, make sure its version [\u003e= v2.15.3](https://github.com/reduxjs/redux/issues/2943) and [not v2.16.0](https://stackoverflow.com/a/53512072/6919133).\n\n### 2. withStore()\n\n`withStore(...modelNames)(Component)`\n\nEject state and actions of one or more models, to the props of a component. 3 ways to use it:\n\n####\n\n```js\n// 1. Use string to eject all\nconst Wrapper = withStore('home', 'counter')(Counter);\n\n// The simplest way, but unused props will also trigger re-render.\n// Use this if all injected props will be used, or to rapid develop.\n```\n\n```js\n// 2. Use object to customize\nconst Wrapper = withStore({\n  home: ['name', 'setName'],\n  counter: ['count', 'add', 'addAsync'],\n})(Counter);\n\n// Customize the injected props, only inject the needed props.\n```\n\n```js\n// 3. Use `mapStateToProps()`... to customize more\nconst Wrapper = withStore(mapStateToProps, mapDispatchToProps)(Counter);\n\n// For more customization of the injected props,\n// use `mapStateToProps`, `mapDispatchToProps` etc.\n// react-redux.js.org/api/connect\n```\n\n### 3. Provider \u0026 batch()\n\nJust `redux-redux`'s [`Provider`](https://react-redux.js.org/api/provider) and [`batch()`](https://react-redux.js.org/api/batch).\n\nYou can import them from `retalk` to simplify development.\n\n## FAQ\n\n### Async import models?\n\nSetup the store with `setStore()`, then use libs like [`loadable-components`](https://github.com/smooth-code/loadable-components/#loading-multiple-resources-in-parallel) to import components and models.\n\nThen, use `store.add()` to eject models to store.\n\nHere is an example with `loadable-components`:\n\n```jsx harmony\nimport React from 'react';\nimport loadable from 'loadable-components';\n\nconst Wrapper = loadable(async () =\u003e {\n  const [{ default: Counter }, { default: CounterModel }] = await Promise.all([\n    import('./Counter/index.jsx'),\n    import('./Counter/Model.js'),\n  ]);\n  store.add({ counter: CounterModel }); // use `store.add(models)` just like `setStore(models)`\n  return (props) =\u003e \u003cCounter {...props} /\u003e;\n});\n```\n\n## License\n\n[MIT](https://github.com/nanxiaobei/retalk/blob/master/LICENSE) © [nanxiaobei](https://lee.so/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanxiaobei%2Fretalk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanxiaobei%2Fretalk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanxiaobei%2Fretalk/lists"}