{"id":21610484,"url":"https://github.com/d-band/yax","last_synced_at":"2025-04-11T05:22:28.877Z","repository":{"id":57403347,"uuid":"88284210","full_name":"d-band/yax","owner":"d-band","description":"Yet another store using redux. (Inspired by vuex and dva)","archived":false,"fork":false,"pushed_at":"2020-06-01T01:23:35.000Z","size":44,"stargazers_count":65,"open_issues_count":1,"forks_count":2,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-28T03:17:12.211Z","etag":null,"topics":["react","react-router","redux","store"],"latest_commit_sha":null,"homepage":null,"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/d-band.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}},"created_at":"2017-04-14T16:30:09.000Z","updated_at":"2023-05-22T19:54:51.000Z","dependencies_parsed_at":"2022-09-19T01:11:51.901Z","dependency_job_id":null,"html_url":"https://github.com/d-band/yax","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/d-band%2Fyax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-band%2Fyax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-band%2Fyax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d-band%2Fyax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/d-band","download_url":"https://codeload.github.com/d-band/yax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248346363,"owners_count":21088445,"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":["react","react-router","redux","store"],"created_at":"2024-11-24T21:08:21.294Z","updated_at":"2025-04-11T05:22:28.855Z","avatar_url":"https://github.com/d-band.png","language":"JavaScript","funding_links":[],"categories":["Marks"],"sub_categories":["[React - A JavaScript library for building user interfaces](http://facebook.github.io/react)"],"readme":"Yax (Yet Another Store)\n=======================\n\n[![NPM version](https://img.shields.io/npm/v/yax.svg)](https://www.npmjs.com/package/yax)\n[![NPM downloads](https://img.shields.io/npm/dm/yax.svg)](https://www.npmjs.com/package/yax)\n[![Build Status](https://travis-ci.org/d-band/yax.svg?branch=master)](https://travis-ci.org/d-band/yax)\n[![Coverage Status](https://coveralls.io/repos/github/d-band/yax/badge.svg?branch=master)](https://coveralls.io/github/d-band/yax?branch=master)\n[![Dependency Status](https://david-dm.org/d-band/yax.svg)](https://david-dm.org/d-band/yax)\n[![Greenkeeper badge](https://badges.greenkeeper.io/d-band/yax.svg)](https://greenkeeper.io/)\n\n\u003e Yet another store using redux. (Inspired by vuex and dva)\n\n## Demos \u0026 Plugins\n\n- [yax-router](https://github.com/d-band/yax-router): Router plugin for yax (Using react-router).\n- [HackerNews](https://github.com/d-band/yax-hackernews): HackerNews clone built with Yax, based on dva-hackernews.\n\n## Getting Started\n\n### Install\n\n```bash\n$ npm install --save yax\n```\n\n### Usage Example\n\n```javascript\nimport yax from 'yax';\n\nconst delay = ms =\u003e new Promise(resolve =\u003e setTimeout(resolve, ms));\n\nconst count = {\n  state: 0,\n  reducers: {\n    addDone (state, payload) {\n      return state + payload;\n    },\n    minusDone (state, payload) {\n      return state - payload;\n    }\n  },\n  actions: {\n    async add ({ commit }, payload) {\n      await delay(1);\n      commit('addDone', payload);\n    },\n    async minus ({ commit }, payload) {\n      await delay(1);\n      commit('minusDone', payload);\n    }\n  }\n};\nconst store = yax({\n  modules: { count }\n});\n\nstore.subscribe(() =\u003e\n  console.log(store.getState())\n);\n\nstore.dispatch({\n  type: 'count/add',\n  payload: 2\n});\nstore.dispatch({\n  type: 'count/minus',\n  payload: 1\n});\n```\n\nUsage with React\n\n```javascript\nimport React from 'react';\nimport { render } from 'react-dom';\nimport { Provider } from 'react-redux';\nimport yax from 'yax';\nimport count from './count';\nimport App from './App';\n\nconst store = yax({\n  modules: { count }\n});\n\nrender(\n  \u003cProvider store={store}\u003e\n    \u003cApp /\u003e\n  \u003c/Provider\u003e,\n  document.getElementById('root')\n);\n```\n\nUsage with `redux-persist`\n\n```javascript\nimport yax from 'yax';\nimport { persistStore, autoRehydrate } from 'redux-persist';\n\nconst store = yax(options, autoRehydrate());\n\npersistStore(store, { storage: Storage });\n```\n\nUsage with `react-navigation`\n\n```javascript\nimport yax, { compose, composeReducers, mapReducers } from 'yax';\nimport { StackNavigator } from 'react-navigation';\n\nconst AppNavigator = StackNavigator(AppRouteConfigs);\nconst initialAction = AppNavigator.router.getActionForPathAndParams('Login');\nconst initialState = AppNavigator.router.getStateForAction(initialAction);\n\nconst navReducer = (state = initialState, action) =\u003e {\n  const nextState = AppNavigator.router.getStateForAction(action, state);\n  return nextState || state;\n};\nconst navEnhancer = createStore =\u003e (reducer, preloadedState, enhancer) =\u003e {\n  const appReducer = composeReducers(\n    reducer,\n    mapReducers({\n      nav: navReducer\n    })\n  );\n  return createStore(appReducer, preloadedState, enhancer);\n};\nconst store = yax({\n  modules: { foo, bar }\n}, compose(\n  navEnhancer,\n  otherEnhancers\n));\n\n\n// In modules with `commit`\nimport { NavigationActions } from 'react-navigation';\n\nconst LOGIN = NavigationActions.navigate({ routeName: 'Login' });\nconst BACK = NavigationActions.back();\n...\ncommit(LOGIN, true);\ncommit(BACK, true);\n```\n\n## API Reference\n\n```javascript\nimport yax, {\n  // Redux original functions\n  combineReducers,\n  bindActionCreators,\n  applyMiddleware,\n  compose,\n  // Yax helper functions\n  composeReducers,\n  mapReducers,\n  mapState,\n  mapActions\n} from 'yax';\n```\n\n### `yax(options = {}, [enhancer])`\n\n\n- **`options.state`**\n\n  The root state object for the store.\n  \n- **`options.reducers`**\n\n  ```\n  { [type: string]: Reducer }\n  ```\n  ```\n  type Reducer = (state, payload) =\u003e state\n  ```\n  \u003e `state` will be module local state if defined in a module\n  \n- **`options.actions`**\n\n  ```\n  { [type: string]: Action }\n  ```\n  ```\n  type Action = ({ dispatch, commit, select }, payload) =\u003e Promise\n  ```\n  ```\n  type dispatch = (type, payload, isRoot) =\u003e Promise\n  type commit   = (type, payload, isRoot) =\u003e any\n  ```\n  \u003e `isRoot=true` will dispatch actions or commit reducers in the global namespace\n  \n  ```\n  type select   = (Selector) =\u003e Selector(state, rootState)\n  type Selector = (state, rootState) =\u003e any\n  ```\n  \u003e `select()` without `Selector` will return `state`\n  \n- **`options.modules`**\n\n  ```\n  { [type: string]: Module }\n  ```\n  ```\n  type Module = { state, reducers, actions, modules }\n  ```\n  \n- **`enhancer`**\n\n  ```\n  type StoreEnhancer = (next: StoreCreator) =\u003e StoreCreator\n  ```\n  \u003e [redux store enhancer](http://redux.js.org/docs/Glossary.html#store-enhancer)\n\n### `composeReducers(...reducers)`\n\n```javascript\nconst f = (state, action) =\u003e {};\nconst g = (state, action) =\u003e {};\n// (state, action) =\u003e g(f(state, action), action)\nconst reducer = composeReducers(f, g);\n```\n\n### `mapReducers(reducers)`\n\n\u003e Like with `combineReducers` but composability. [More](https://github.com/reactjs/redux/pull/2059#issuecomment-256798218)\n\n```javascript\nconst foo = (state, action) =\u003e {};\nconst bar = (state, action) =\u003e {};\nconst reducer = mapReducers({ foo, bar });\n```\n\n### `mapState` and `mapActions`\n\n\u003e Used for [react-redux `connect`](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)\n\n```\nconst App = ({ foo, bar, addFoo, addBar }) =\u003e {\n  addFoo(payload);\n  addBar(payload);\n  return \u003cspan\u003e{foo} / {bar}\u003c/span\u003e;\n};\nconnect(\n  mapState({\n    foo: 'foo/value',\n    bar: 'bar/value'\n  }),\n  mapActions({\n    addFoo: 'foo/add',\n    addBar: 'bar/add'\n  })\n)(App);\n```\n\n## Report a issue\n\n* [All issues](https://github.com/d-band/yax/issues)\n* [New issue](https://github.com/d-band/yax/issues/new)\n\n## License\n\nyax is available under the terms of the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-band%2Fyax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd-band%2Fyax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd-band%2Fyax/lists"}