{"id":13394351,"url":"https://github.com/developit/unistore","last_synced_at":"2025-05-14T05:11:22.625Z","repository":{"id":38455613,"uuid":"113351554","full_name":"developit/unistore","owner":"developit","description":"🌶 350b / 650b state container with component actions for Preact \u0026 React","archived":false,"fork":false,"pushed_at":"2021-06-07T10:00:27.000Z","size":172,"stargazers_count":2853,"open_issues_count":46,"forks_count":137,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-05-13T22:26:55.103Z","etag":null,"topics":["architecture","preact","redux","state","subscription","unistore"],"latest_commit_sha":null,"homepage":"https://npm.im/unistore","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":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-06T18:15:34.000Z","updated_at":"2025-05-09T21:31:45.000Z","dependencies_parsed_at":"2022-08-09T04:16:53.831Z","dependency_job_id":null,"html_url":"https://github.com/developit/unistore","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Funistore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Funistore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Funistore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Funistore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/unistore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254051340,"owners_count":22006463,"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":["architecture","preact","redux","state","subscription","unistore"],"created_at":"2024-07-30T17:01:16.784Z","updated_at":"2025-05-14T05:11:17.600Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/o0u6dto.png\" width=\"300\" height=\"300\" alt=\"unistore\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.org/package/unistore\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/unistore.svg?style=flat\" alt=\"npm\"\u003e\u003c/a\u003e \u003ca href=\"https://travis-ci.org/developit/unistore\"\u003e\u003cimg src=\"https://travis-ci.org/developit/unistore.svg?branch=master\" alt=\"travis\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# unistore\n\n\u003e A tiny 350b centralized state container with component bindings for [Preact] \u0026 [React].\n\n- **Small** footprint complements Preact nicely _(unistore + unistore/preact is ~650b)_\n- **Familiar** names and ideas from Redux-like libraries\n- **Useful** data selectors to extract properties from state\n- **Portable** actions can be moved into a common place and imported\n- **Functional** actions are just reducers\n- **NEW**: seamlessly run Unistore in a worker via [Stockroom](https://github.com/developit/stockroom)\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n- [Examples](#examples)\n- [API](#api)\n- [License](#license)\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 unistore\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\n// The store:\nimport createStore from 'unistore'\n\n// Preact integration\nimport { Provider, connect } from 'unistore/preact'\n\n// React integration\nimport { Provider, connect } from 'unistore/react'\n```\n\nAlternatively, you can import the \"full\" build for each, which includes both `createStore` and the integration for your library of choice:\n\n```js\nimport { createStore, Provider, connect } from 'unistore/full/preact'\n```\n\nThe [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):\n\n```html\n\u003c!-- just unistore(): --\u003e\n\u003cscript src=\"https://unpkg.com/unistore/dist/unistore.umd.js\"\u003e\u003c/script\u003e\n\u003c!-- for preact --\u003e\n\u003cscript src=\"https://unpkg.com/unistore/full/preact.umd.js\"\u003e\u003c/script\u003e\n\u003c!-- for react --\u003e\n\u003cscript src=\"https://unpkg.com/unistore/full/react.umd.js\"\u003e\u003c/script\u003e\n```\n\nYou can find the library on `window.unistore`.\n\n### Usage\n\n```js\nimport createStore from 'unistore'\nimport { Provider, connect } from 'unistore/preact'\n\nlet store = createStore({ count: 0, stuff: [] })\n\nlet actions = {\n  // Actions can just return a state update:\n  increment(state) {\n    // The returned object will be merged into the current state\n    return { count: state.count+1 }\n  },\n\n  // The above example as an Arrow Function:\n  increment2: ({ count }) =\u003e ({ count: count+1 }),\n\n  // Actions receive current state as first parameter and any other params next\n  // See the \"Increment by 10\"-button below\n  incrementBy: ({ count }, incrementAmount) =\u003e {\n    return { count: count+incrementAmount }\n  },\n}\n\n// If actions is a function, it gets passed the store:\nlet actionFunctions = store =\u003e ({\n  // Async actions can be pure async/promise functions:\n  async getStuff(state) {\n    const res = await fetch('/foo.json')\n    return { stuff: await res.json() }\n  },\n\n  // ... or just actions that call store.setState() later:\n  clearOutStuff(state) {\n    setTimeout(() =\u003e {\n      store.setState({ stuff: [] }) // clear 'stuff' after 1 second\n    }, 1000)\n  }\n\n  // Remember that the state passed to the action function could be stale after\n  // doing async work, so use getState() instead:\n  async incrementAfterStuff(state) {\n    const res = await fetch('foo.json')\n    const resJson = await res.json()\n    // the variable 'state' above could now be old,\n    // better get a new one from the store\n    const upToDateState = store.getState()\n\n    return {\n      stuff: resJson,\n      count: upToDateState.count + resJson.length,\n    }\n  }\n})\n\n// Connecting a react/preact component to get current state and to bind actions\nconst App1 = connect('count', actions)(\n  ({ count, increment, incrementBy }) =\u003e (\n    \u003cdiv\u003e\n      \u003cp\u003eCount: {count}\u003c/p\u003e\n      \u003cbutton onClick={increment}\u003eIncrement\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e incrementBy(10)}\u003eIncrement by 10\u003c/button\u003e\n    \u003c/div\u003e\n  )\n)\n\n// First argument to connect can also be a string, array or function while\n// second argument can be an object or a function. Here we pass an array and\n// a function.\nconst App2 = connect(['count', 'stuff'], actionFunctions)(\n  ({ count, stuff, getStuff, clearOutStuff, incrementAfterStuff }) =\u003e (\n    \u003cdiv\u003e\n      \u003cp\u003eCount: {count}\u003c/p\u003e\n      \u003cp\u003eStuff:\n        \u003cul\u003e{stuff.map(s =\u003e (\n         \u003cli\u003e{s.name}\u003c/li\u003e\n        ))}\u003c/ul\u003e\n      \u003c/p\u003e\n      \u003cbutton onClick={getStuff}\u003eGet some stuff!\u003c/button\u003e\n      \u003cbutton onClick={clearOutStuff}\u003eRemove all stuff!\u003c/button\u003e\n      \u003cbutton onClick={incrementAfterStuff}\u003eGet and count stuff!\u003c/button\u003e\n    \u003c/div\u003e\n  )\n)\n\nexport const getApp1 = () =\u003e (\n  \u003cProvider store={store}\u003e\n    \u003cApp1 /\u003e\n  \u003c/Provider\u003e\n)\n\nexport const getApp2 = () =\u003e (\n  \u003cProvider store={store}\u003e\n    \u003cApp2 /\u003e\n  \u003c/Provider\u003e\n)\n```\n\n### Debug\n\nMake sure to have [Redux devtools extension](https://github.com/zalmoxisus/redux-devtools-extension) previously installed.\n\n```js\nimport createStore from 'unistore'\nimport devtools    from 'unistore/devtools'\n\nlet initialState = { count: 0 };\nlet store = process.env.NODE_ENV === 'production' ?  createStore(initialState) : devtools(createStore(initialState));\n\n// ...\n```\n\n### Examples\n\n[README Example on CodeSandbox](https://codesandbox.io/s/l7y7w5qkz9)\n\n### API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n#### createStore\n\nCreates a new store, which is a tiny evented state container.\n\n**Parameters**\n\n- `state` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional initial state (optional, default `{}`)\n\n**Examples**\n\n```javascript\nlet store = createStore();\nstore.subscribe( state =\u003e console.log(state) );\nstore.setState({ a: 'b' });   // logs { a: 'b' }\nstore.setState({ c: 'd' });   // logs { a: 'b', c: 'd' }\n```\n\nReturns **[store](#store)** \n\n#### store\n\nAn observable state container, returned from [createStore](#createstore)\n\n##### action\n\nCreate a bound copy of the given action function.\nThe bound returned function invokes action() and persists the result back to the store.\nIf the return value of `action` is a Promise, the resolved value will be used as state.\n\n**Parameters**\n\n- `action` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** An action of the form `action(state, ...args) -\u003e stateUpdate`\n\nReturns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** boundAction()\n\n##### setState\n\nApply a partial state object to the current state, invoking registered listeners.\n\n**Parameters**\n\n- `update` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object with properties to be merged into state\n- `overwrite` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If `true`, update will replace state instead of being merged into it (optional, default `false`)\n\n##### subscribe\n\nRegister a listener function to be called whenever state is changed. Returns an `unsubscribe()` function.\n\n**Parameters**\n\n- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** A function to call when state changes. Gets passed the new state.\n\nReturns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** unsubscribe()\n\n##### unsubscribe\n\nRemove a previously-registered listener function.\n\n**Parameters**\n\n- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The callback previously passed to `subscribe()` that should be removed.\n\n##### getState\n\nRetrieve the current state object.\n\nReturns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** state\n\n#### connect\n\nWire a component up to the store. Passes state as props, re-renders on change.\n\n**Parameters**\n\n- `mapStateToProps` **([Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) \\| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \\| [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** A function mapping of store state to prop values, or an array/CSV of properties to map.\n- `actions` **([Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) \\| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))?** Action functions (pure state mappings), or a factory returning them. Every action function gets current state as the first parameter and any other params next\n\n**Examples**\n\n```javascript\nconst Foo = connect('foo,bar')( ({ foo, bar }) =\u003e \u003cdiv /\u003e )\n```\n\n```javascript\nconst actions = { someAction }\nconst Foo = connect('foo,bar', actions)( ({ foo, bar, someAction }) =\u003e \u003cdiv /\u003e )\n```\n\nReturns **Component** ConnectedComponent\n\n#### Provider\n\n**Extends Component**\n\nProvider exposes a store (passed as `props.store`) into context.\n\nGenerally, an entire application is wrapped in a single `\u003cProvider\u003e` at the root.\n\n**Parameters**\n\n- `props` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** \n    -   `props.store` **Store** A {Store} instance to expose via context.\n\n### Reporting Issues\n\nFound a problem? Want a new feature? First of all, see if your issue or idea has [already been reported](../../issues).\nIf not, just open a [new clear and descriptive issue](../../issues/new).\n\n### License\n\n[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com)\n\n[preact]: https://github.com/developit/preact\n\n[react]: https://github.com/facebook/react\n","funding_links":[],"categories":["JavaScript","react","Packages","State Managers","Frameworks","List","Uncategorized"],"sub_categories":["State of the React","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Funistore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Funistore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Funistore/lists"}