{"id":13429609,"url":"https://github.com/philipp-spiess/use-substate","last_synced_at":"2025-10-18T01:43:24.575Z","repository":{"id":57388430,"uuid":"154746338","full_name":"philipp-spiess/use-substate","owner":"philipp-spiess","description":"🍙 Lightweight (\u003c600B minified + gzipped) React Hook to subscribe to a subset of your single app state.","archived":false,"fork":false,"pushed_at":"2018-11-17T14:13:41.000Z","size":89,"stargazers_count":100,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-01T02:42:36.194Z","etag":null,"topics":["hooks","react","react-hooks","reducer","redux","state-management","store","substate"],"latest_commit_sha":null,"homepage":"https://github.com/philipp-spiess/use-substate","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/philipp-spiess.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-10-25T22:41:41.000Z","updated_at":"2025-02-06T00:40:45.000Z","dependencies_parsed_at":"2022-09-15T01:56:38.001Z","dependency_job_id":null,"html_url":"https://github.com/philipp-spiess/use-substate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/philipp-spiess/use-substate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-spiess%2Fuse-substate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-spiess%2Fuse-substate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-spiess%2Fuse-substate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-spiess%2Fuse-substate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philipp-spiess","download_url":"https://codeload.github.com/philipp-spiess/use-substate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipp-spiess%2Fuse-substate/sbom","scorecard":{"id":731381,"data":{"date":"2025-08-11","repo":{"name":"github.com/philipp-spiess/use-substate","commit":"845db7cd2c423a7f4eb58261a44f1496e623f477"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 1/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 1 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"23 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-6chw-6frg-f759","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx","Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-257v-vj4p-3w2h","Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm","Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr","Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg","Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546","Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp","Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-566m-qj78-rww5","Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j","Warn: Project is vulnerable to: GHSA-hwj9-h5mp-3pm3","Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-22T14:31:43.480Z","repository_id":57388430,"created_at":"2025-08-22T14:31:43.480Z","updated_at":"2025-08-22T14:31:43.480Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279448242,"owners_count":26171924,"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","status":"online","status_checked_at":"2025-10-17T02:00:07.504Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["hooks","react","react-hooks","reducer","redux","state-management","store","substate"],"created_at":"2024-07-31T02:00:42.462Z","updated_at":"2025-10-18T01:43:24.521Z","avatar_url":"https://github.com/philipp-spiess.png","language":"JavaScript","funding_links":[],"categories":["Packages","JavaScript"],"sub_categories":[],"readme":"# 🍙 useSubstate\n\nLightweight ([\u003c600B minified + gzipped](https://bundlephobia.com/result?p=use-substate)) React Hook to **sub**scribe to a **sub**set of your single app state.\n\n\n```js\nfunction Component() {\n  const [substate, dispatch] = useSubstate(state =\u003e {\n    return { count: state.count };\n  });\n\n  return (\n    \u003cdiv\u003e\n      {substate.count}\n\n      \u003cbutton\n        onClick={() =\u003e dispatch({ type: \"INCREMENT\" })}\n      \u003e\n        Increment\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n## Features\n\n- ⏬ Lightweight ([\u003c600B minified + gzipped](https://bundlephobia.com/result?p=use-substate))\n- ⚛️ Works with your existing Redux-like store\n- 🙅‍♀️ Avoids unnecessary re-renders\n- 🔂 Uses an external event subscription to short-circuit context propagation (Making it Concurrent React _Unsafe_)\n- 🎈 Full Flow and TypeScript support coming soon\n- 🎮 You’re in full control of your store and can use it outside React as well\n\n## Requirements\n\n__⚠️ To use `useSubstate`, you will need the unstable and experimental React 16.7.0-alpha. Check out the [official documentation](https://reactjs.org/docs/hooks-intro.html) or [this blog post](https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889) by Dan Abramov for more information.__\n\n`useSubstate` can also be used together with [react-redux][] in your existing [Redux][] application. Check out [Comparison To Redux](#comparison-to-redux) for more information.\n\n## Installation\n\n```bash\nnpm install --save use-substate\n```\n\n## Usage\n\nYou can use `useSubstate` with your existing [Redux][] store or with a simple alternative (like [create-store](https://github.com/philipp-spiess/create-store)). This package will export a [React Context](https://reactjs.org/docs/context.html) consumer (`SubstateContext`) as well the `useSubstate` hook.\n\nThis custom hook will expose an API similar to [`useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer). The only argument for `useSubstate` is a `selectSubstate` function which is used to select parts of your state to be used within the component that uses the hook. This allows `useSubstate` to bail out if unnecessary parts change. Every component that uses this custom hook will automatically subscribe to the store.\n\nThe example below will show all steps necessary to use `useSubstate`:\n\n```js\nimport React, { useState } from \"react\";\nimport ReactDOM from \"react-dom\";\n\nimport createStore from \"create-store\";\nimport { SubstateProvider, useSubstate } from \"use-substate\";\n\nconst initialState = { count: 0 };\nfunction reducer(state, action) {\n  switch (action.type) {\n    case \"INCREMENT\":\n      return { count: state.count + 1 };\n    case \"DECREMENT\":\n      return { count: state.count - 1 };\n  }\n}\nconst store = createStore(reducer, initialState);\n\nfunction App() {\n  const [substate, dispatch] = useSubstate(state =\u003e {\n    return { count: state.count };\n  });\n\n  return (\n    \u003cdiv\u003e\n      {substate.count}\n\n      \u003cbutton onClick={() =\u003e dispatch({ type: \"INCREMENT\" })}\u003eIncrement\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e dispatch({ type: \"DECREMENT\" })}\u003eDecrement\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n\nReactDOM.render(\n  \u003cSubstateProvider value={store}\u003e\n    \u003cApp /\u003e\n  \u003c/SubstateProvider\u003e,\n  rootElement\n);\n```\n\n[![Edit useSubstate Example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/4w406kwy44)\n\n## Comparison To Redux\n\n[Redux][] is a library used to create stores that can be updated via reducers. In fact, `useSubstate` works flawlessly with your current Redux store.\n\nIn opposite to [react-redux][], this library only requires a `selectSubstate` function (similar to [react-redux][]'s `mapStateToProps`). It is meant to call the `dispatch` function with the action directly. Advanced concepts like `connectAdvanced` or `mapDispatchToProps` are deliberately not supported.\n\nTo use `useSubstate` with your current [react-redux][] React application, find the react-redux `Provider` and make sure to wrap it with a `SubstateProvider`:\n\n```diff\nimport React from \"react\";\nimport { render } from \"react-dom\";\nimport { Provider } from \"react-redux\";\n+ import { SubstateProvider } from \"use-substate\";\nimport { createStore } from \"redux\";\nimport todoApp from \"./reducers\";\nimport App from \"./components/App\";\n\nconst store = createStore(todoApp);\n\nrender(\n+ \u003cSubstateProvider value={store}\u003e\n    \u003cProvider store={store}\u003e\n      \u003cApp /\u003e\n    \u003c/Provider\u003e\n+ \u003c/SubstateProvider\u003e,\n  document.getElementById(\"root\")\n);\n```\n\n## Other Libraries\n\nBesides the [open issue in `react-redux`](https://github.com/reduxjs/react-redux/issues/1063), there are two other noticeable libraries that solve the a similiar problem:\n\n- [`redux-react-hook`](https://github.com/facebookincubator/redux-react-hook)\n- [`react-use-redux`](https://github.com/martynaskadisa/react-use-redux)\n\n## Acknowledgements\n\nSpecial thanks to [@sophiebits](https://github.com/sophiebits) and [@gaearon](https://github.com/gaearon) for helping me spot an issue in my initial implementation and showing me how to fix it.\n\nThis library is also heavily influenced by the design of [`useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer), [create-subscription](https://github.com/facebook/react/tree/master/packages/create-subscription), [react-redux](https://github.com/reduxjs/react-redux), [Reducer components](https://reasonml.github.io/reason-react/docs/en/state-actions-reducer.html) in ReasonReact, [Elm](http://elm-lang.org/), [Reagent (Clojure)](https://reagent-project.github.io/), [Om (Clojure)](https://github.com/omcljs/om), and a lot of other libraries that I have seen over the years. Thank you all for pushing the Web forward ❤️.\n\n## Contributing\n\nEvery help on this project is greatly appreciated. To get you started, here's a quick guide on how to make good and clean pull-requests:\n\n1.  Create a fork of this [repository](https://github.com/philipp-spiess/use-substate), so you can work on your own environment.\n2.  Install development dependencies locally:\n\n    ```bash\n    git clone git@github.com:\u003cyour-github-name\u003e/use-substate.git\n    cd use-substate\n    yarn install\n    ```\n\n3.  Make changes using your favorite editor.\n4.  Commit your changes ([here](https://chris.beams.io/posts/git-commit/) is a wonderful guide on how to make amazing git commits).\n5.  After a few seconds, a button to create a pull request should be visible inside the [Pull requests](https://github.com/philipp-spiess/use-substate/pulls) section.\n\n## Future Improvements\n\n- [ ] Add Flow and TypeScript types. This is actually very important for this library: Actions must be typed as an enum such that the type system can find out if we use the wrong type.\n- [ ] Improve test harness.\n\n## License\n\n[MIT](https://github.com/philipp-spiess/use-substate/blob/master/README.md)\n\n[Redux]: https://redux.js.org/introduction\n[react-redux]: https://github.com/reduxjs/react-redux\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipp-spiess%2Fuse-substate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilipp-spiess%2Fuse-substate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipp-spiess%2Fuse-substate/lists"}