{"id":13559368,"url":"https://github.com/ionic-team/stencil-redux","last_synced_at":"2025-10-08T03:31:01.989Z","repository":{"id":39636777,"uuid":"97962383","full_name":"ionic-team/stencil-redux","owner":"ionic-team","description":null,"archived":true,"fork":false,"pushed_at":"2022-06-24T17:31:25.000Z","size":1186,"stargazers_count":97,"open_issues_count":28,"forks_count":20,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-01-18T21:58:42.470Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/ionic-team.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-07-21T15:29:29.000Z","updated_at":"2023-11-20T13:27:09.000Z","dependencies_parsed_at":"2022-09-13T03:10:39.408Z","dependency_job_id":null,"html_url":"https://github.com/ionic-team/stencil-redux","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-redux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-redux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-redux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-redux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ionic-team","download_url":"https://codeload.github.com/ionic-team/stencil-redux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235678929,"owners_count":19028301,"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":[],"created_at":"2024-08-01T13:00:20.161Z","updated_at":"2025-10-08T03:31:01.606Z","avatar_url":"https://github.com/ionic-team.png","language":"TypeScript","readme":"## Stencil Redux\n\nA simple redux connector for Stencil-built web components inspired by [`react-redux`](https://github.com/reduxjs/react-redux).\n\n## Install\n\n```\nnpm install @stencil/redux\nnpm install redux\n```\n\n## Usage\n\nStencil Redux uses the [`redux`](https://github.com/reduxjs/redux/) library underneath. Setting up the store and defining actions, reducers, selectors, etc. should be familiar to you if you've used React with Redux.\n\n### Configure the Root Reducer\n\n```typescript\n// redux/reducers.ts\n\nimport { combineReducers } from 'redux';\n\n// Import feature reducers and state interfaces.\nimport { TodoState, todos } from './todos/reducers';\n\n// This interface represents app state by nesting feature states.\nexport interface RootState {\n  todos: TodoState;\n}\n\n// Combine feature reducers into a single root reducer\nexport const rootReducer = combineReducers({\n  todos,\n});\n```\n\n### Configure the Actions\n\n```typescript\n// redux/actions.ts\n\nimport { RootState } from './reducers';\n\n// Import feature action interfaces\nimport { TodoAction } from './todos/actions';\n\n// Export all feature actions for easier access.\nexport * from './todos/actions';\n\n// Combine feature action interfaces into a base type. Use union types to\n// combine feature interfaces.\n// https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types\nexport type Action = (\n  TodoAction\n);\n```\n\n### Configure the Store\n\n```typescript\n// redux/store.ts\n\nimport { Store, applyMiddleware, createStore } from 'redux';\nimport thunk from 'redux-thunk'; // add-on you may want\nimport logger from 'redux-logger'; // add-on you may want\n\nimport { RootState, rootReducer } from './reducers';\n\nexport const store: Store\u003cRootState\u003e = createStore(rootReducer, applyMiddleware(thunk, logger));\n```\n\n### Configure Store in Root Component\n\n```typescript\n\n// components/my-app/my-app.tsx\n\nimport { store } from '@stencil/redux';\n\nimport { Action } from '../../redux/actions';\nimport { RootState } from '../../redux/reducers';\nimport { initialStore } from '../../redux/store';\n\n@Component({\n  tag: 'my-app',\n  styleUrl: 'my-app.scss'\n})\nexport class MyApp {\n\n  componentWillLoad() {\n    store.setStore(initialStore);\n  }\n\n}\n```\n\n### Map state and dispatch to props\n\n:memo: *Note*: Because the mapped props are technically changed *within* the component, `mutable: true` is required for `@Prop` definitions that utilize the store. See the [Stencil docs](https://stenciljs.com/docs/properties#prop-value-mutability) for info.\n\n```typescript\n// components/my-component/my-component.tsx\n\nimport { store, Unsubscribe } from '@stencil/redux';\n\nimport { Action, changeName } from '../../redux/actions';\nimport { RootState } from '../../redux/reducers';\n\n@Component({\n  tag: 'my-component',\n  styleUrl: 'my-component.scss'\n})\nexport class MyComponent {\n  @Prop({ mutable: true }) name: string;\n\n  changeName!: typeof changeName;\n\n  unsubscribe!: Unsubscribe;\n\n  componentWillLoad() {\n    this.unsubscribe = store.mapStateToProps(this, state =\u003e {\n      const { user: { name } } = state;\n      return { name };\n    });\n\n    store.mapDispatchToProps(this, { changeName });\n  }\n\n  componentDidUnload() {\n    this.unsubscribe();\n  }\n\n  doNameChange(newName: string) {\n    this.changeName(newName);\n  }\n}\n```\n\n### Usage with `redux-thunk`\n\nSome Redux middleware, such as `redux-thunk`, alter the store's `dispatch()` function, resulting in type mismatches with mapped actions in your components.\n\nTo properly type mapped actions in your components (properties whose values are set by `store.mapDispatchToProps()`), you can use the following type:\n\n```typescript\nimport { ThunkAction } from 'redux-thunk';\n\nexport type Unthunk\u003cT\u003e = T extends (...args: infer A) =\u003e ThunkAction\u003cinfer R, any, any, any\u003e\n  ? (...args: A) =\u003e R\n  : T;\n```\n\n#### Example\n\n```typescript\n// redux/user/actions.ts\n\nimport { ThunkAction } from 'redux-thunk';\n\nexport const changeName = (name: string): ThunkAction\u003cPromise\u003cvoid\u003e, RootState, void, Action\u003e =\u003e async (dispatch, getState) =\u003e {\n  await fetch(...); // some async operation\n};\n```\n\nIn the component below, the type of `this.changeName` is extracted from the action type to be `(name: string) =\u003e Promise\u003cvoid\u003e`.\n\n```typescript\n// components/my-component/my-component.tsx\n\nimport { changeName } from '../../redux/actions';\n\nexport class MyComponent {\n  changeName!: Unthunk\u003ctypeof changeName\u003e;\n\n  componentWillLoad() {\n    store.mapDispatchToProps(this, { changeName });\n  }\n}\n```\n","funding_links":[],"categories":["Ecosystem Highlights",":atom_symbol: Redux"],"sub_categories":["State Management","Redux Tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionic-team%2Fstencil-redux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fionic-team%2Fstencil-redux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionic-team%2Fstencil-redux/lists"}