{"id":21444682,"url":"https://github.com/9technology/redux-namespaces","last_synced_at":"2025-07-14T18:31:37.926Z","repository":{"id":44142051,"uuid":"94746160","full_name":"9technology/redux-namespaces","owner":"9technology","description":"Namespaces for Redux actions, reducers and state.","archived":false,"fork":false,"pushed_at":"2023-01-03T22:17:14.000Z","size":3087,"stargazers_count":7,"open_issues_count":18,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-04-08T17:00:30.942Z","etag":null,"topics":["namespaces","react-redux","redux"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/9technology.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":"2017-06-19T06:59:12.000Z","updated_at":"2020-09-07T15:02:54.000Z","dependencies_parsed_at":"2023-02-01T11:16:29.898Z","dependency_job_id":null,"html_url":"https://github.com/9technology/redux-namespaces","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/9technology%2Fredux-namespaces","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/9technology%2Fredux-namespaces/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/9technology%2Fredux-namespaces/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/9technology%2Fredux-namespaces/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/9technology","download_url":"https://codeload.github.com/9technology/redux-namespaces/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225991533,"owners_count":17556326,"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":["namespaces","react-redux","redux"],"created_at":"2024-11-23T02:20:47.951Z","updated_at":"2024-11-23T02:20:48.552Z","avatar_url":"https://github.com/9technology.png","language":"JavaScript","readme":"# Redux Namespaces\n\n[![Build Status](https://travis-ci.org/9technology/redux-namespaces.svg?branch=master)](https://travis-ci.org/9technology/redux-namespaces) [![Coverage Status](https://coveralls.io/repos/github/9technology/redux-namespaces/badge.svg)](https://coveralls.io/github/9technology/redux-namespaces)\n\nNamespace Redux actions, reducers and state.\n\n## Usage\n---\n\nBuild re-usable components in React, or any other UI library, using Redux adds complexity. Namespacing actions, reducers and state allows multiple instances of components to co-exist with independent state.\n\nRead more: [https://www.nine.com.au/tech/2017/07/13/09/40/redux-namespaces](https://www.nine.com.au/tech/2017/07/13/09/40/redux-namespaces)\n\n### Reducers\n\nWhen handling multiple components and computing derived data an application's reducer must be aware of this nature. Usually when reducing the action it passes some meta data. This meta data sometimes changes the returning state structure.\n\nUsing `createReducer()`, reducers can be written without the need of this meta data. For example, our state has two superhereos and the application sets their side kick. The reducer is written as if it only had a single superhero. So no longer do reducers have to be instance aware. Regardless if it's batman or superman the reducer will set the side kick.\n\n```javascript\nimport { createStore } from 'redux';\nimport { createReducer } from 'redux-namespaces';\n\n// Always set the initial state, see Caveats below\nconst initial = {\n  batman: {},\n  superman: {}\n};\n\n// Generic superhero reducer\nconst superheroReducer = (state, action) =\u003e {\n  switch (action.type) {\n    case 'SET_SIDE_KICK':\n      return {\n        ...state,\n        sideKick: action.sideKick,\n      };\n    default:\n      return state;\n  }\n};\n\n// Make the generic reducer namespace aware\nconst reducer = createReducer(superheroReducer);\nconst store = createStore(reducer, initial);\n```\n\n### Action Creators\n\nAction creators now don't require the meta data for the reducer. Since the reducer is built for a single superhero actions can now follow the same pattern.\n\n```javascript\nconst actions = {\n  sideKick(sideKick) {\n    return {\n      type: 'SET_SIDE_KICK',\n      sideKick,\n    };\n  }\n};\n```\n\nUse `createActions()` with a given state namespace and the actions list to generate action creators.\n\n```javascript\nimport { createActions } from 'redux-namespaces';\n\n// Namespace actions\nconst batman = createActions('batman', actions);\nconst superman = createActions('superman', actions);\n```\n\n`batman` and `superman` can now disptach the same actions to the reducer but state independent.\n\n```javascript\n// Dispatch\nstore.dispatch(batman.sideKick('Robin'));\nstore.dispatch(superman.sideKick('Jimmy Olsen'));\n\nstore.getState(); // { batman: { sideKick: 'Robin' }, superman: { sideKick: 'Jimmy Olsen'} }\n```\n\n### React\n\nCombine Redux Namespaces with React following the usual `react-redux` setup. However use `createActions` for `mapDispatchToProps()`.\n\n```javascript\nconst mapDispatchToProps = dispatch =\u003e\n  bindActionCreators(\n    createActions('namespace', {\n      propAction() {\n        return { type: 'action' };\n      }\n    })\n  );\n```\n\n### Caveats\n---\n\nInitial namespaces must be set within the initial state otherwise action creators will dispatch to an undefined property within the state.\n\n## API\n---\n\n#### `createActions(namespace, actions)`\n\n- `namespace` _String_ The namespace for the action creators.\n- `actions` _Object_ Action creators to namespace.\n\n_Returns namespaced action creators._\n\n#### `createReducers(...reducers)`\n\n- `reducers` _Function|[]Function_ Redux reducer.\n\n_Returns wrapped reducer that is namespace aware._\n\n## License\n---\n\n[BSD-3-Clause](LICENSE)\n\nCopyright (c) 2017 [9Technology](https://github.com/9technology)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F9technology%2Fredux-namespaces","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F9technology%2Fredux-namespaces","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F9technology%2Fredux-namespaces/lists"}