UNPKG

8.18 kBMarkdownView Raw
1# Which Version to use?
2
3## Angular 6+
4
5`@angular-redux/store@^9` is what you need. This consumes breaking changes from RxJS and Angular 6, as well as updated typedefs from Redux 4.
6
7## Angular 5
8
9Use `@angular-redux/store@^7` - this version supports Angular 5, and also changes to using lettable operators.
10
11## Angular 4 or lower
12
13Use `@angular-redux/store@^6` - This supports Angular 4 and earlier.
14
15# Support for `@angular-redux/store@6`?
16
17Where possible, I will be maintaining and applying any fixes / enhancements for v7 into v6 where it does not introduce a breaking change.
18
19I made a few mistakes trying to publish fixes / etc to two major versions, which caused some releases to get tagged incorrectly and caused some confusion. Sorry for any confusion this has caused, and will do better on avoiding this in the future, and being more transparent with the releases that are going out.
20
21# @angular-redux/store
22
23Angular bindings for [Redux](https://github.com/reactjs/redux).
24
25For Angular 1 see [ng-redux](https://github.com/wbuchwalter/ng-redux)
26
27[![Join the chat at https://gitter.im/angular-redux/ng2-redux](https://badges.gitter.im/angular-redux/ng2-redux.svg)](https://gitter.im/angular-redux/ng2-redux?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
28[![CircleCI](https://img.shields.io/circleci/project/github/angular-redux/store.svg)](https://github.com/angular-redux/store)
29[![npm version](https://img.shields.io/npm/v/@angular-redux/store.svg)](https://www.npmjs.com/package/@angular-redux/store)
30[![downloads per month](https://img.shields.io/npm/dm/@angular-redux/store.svg)](https://www.npmjs.com/package/@angular-redux/store)
31
32## What is Redux?
33
34Redux is a popular approach to managing state in applications. It emphasises:
35
36- A single, immutable data store.
37- One-way data flow.
38- An approach to change based on pure functions and a stream of actions.
39
40You can find lots of excellent documentation here: [Redux](http://redux.js.org/).
41
42## What is @angular-redux?
43
44We provide a set of npm packages that help you integrate your redux store
45into your Angular 2+ applications. Our approach helps you by bridging the gap
46with some of Angular's advanced features, including:
47
48- Change processing with RxJS observables.
49- Compile time optimizations with `NgModule` and Ahead-of-Time compilation.
50- Integration with the Angular change detector.
51
52## Getting Started
53
54- I already know what Redux and RxJS are. [Give me the TL;DR](articles/quickstart.md).
55- I'm just learning about Redux. [Break it down for me](articles/intro-tutorial.md)!
56- Talk is cheap. [Show me a complete code example](https://github.com/angular-redux/example-app).
57- Take me to the [API docs](https://angular-redux.github.io/store).
58
59## Installation
60
61`@angular-redux/store` has a peer dependency on redux, so we need to install it as well.
62
63```sh
64npm install --save redux @angular-redux/store
65```
66
67## Quick Start
68
69```typescript
70import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
71import { AppModule } from './containers/app.module';
72
73platformBrowserDynamic().bootstrapModule(AppModule);
74```
75
76Import the `NgReduxModule` class and add it to your application module as an
77`import`. Once you've done this, you'll be able to inject `NgRedux` into your
78Angular components. In your top-level app module, you
79can configure your Redux store with reducers, initial state,
80and optionally middlewares and enhancers as you would in Redux directly.
81
82```typescript
83import { NgReduxModule, NgRedux } from '@angular-redux/store';
84import { createLogger } from 'redux-logger';
85import { rootReducer } from './reducers';
86
87interface IAppState {
88 /* ... */
89}
90
91@NgModule({
92 /* ... */
93 imports: [, /* ... */ NgReduxModule],
94})
95export class AppModule {
96 constructor(ngRedux: NgRedux<IAppState>) {
97 ngRedux.configureStore(rootReducer, {}, [createLogger()]);
98 }
99}
100```
101
102Or if you prefer to create the Redux store yourself you can do that and use the
103`provideStore()` function instead:
104
105```typescript
106import {
107 applyMiddleware,
108 Store,
109 combineReducers,
110 compose,
111 createStore,
112} from 'redux';
113import { NgReduxModule, NgRedux } from '@angular-redux/store';
114import { createLogger } from 'redux-logger';
115import { rootReducer } from './reducers';
116
117interface IAppState {
118 /* ... */
119}
120
121export const store: Store<IAppState> = createStore(
122 rootReducer,
123 applyMiddleware(createLogger()),
124);
125
126@NgModule({
127 /* ... */
128 imports: [, /* ... */ NgReduxModule],
129})
130class AppModule {
131 constructor(ngRedux: NgRedux<IAppState>) {
132 ngRedux.provideStore(store);
133 }
134}
135```
136
137> Note that we're also using a Redux middleware from the community here:
138> [redux-logger](https://www.npmjs.com/package/redux-logger). This is just to show
139> off that `@angular-redux/store` is indeed compatible with Redux middlewares as you
140> might expect.
141>
142> Note that to use it, you'll need to install it with `npm install --save redux-logger`
143> and type definitions for it with `npm install --save-dev @types/redux-logger`.
144
145Now your Angular app has been reduxified! Use the `@select` decorator to
146access your store state, and `.dispatch()` to dispatch actions:
147
148```typescript
149import { select } from '@angular-redux/store';
150
151@Component({
152 template:
153 '<button (click)="onClick()">Clicked {{ count | async }} times</button>',
154})
155class App {
156 @select() count$: Observable<number>;
157
158 constructor(private ngRedux: NgRedux<IAppState>) {}
159
160 onClick() {
161 this.ngRedux.dispatch({ type: INCREMENT });
162 }
163}
164```
165
166## Examples
167
168Here are some examples of the `angular-redux` family of packages in action:
169
170- [Zoo Animals Combined Example App](https://github.com/angular-redux/platform/blob/master/packages/example-app)
171
172## Companion Packages
173
174- [Reduxify your Routing with @angular-redux/router](https://github.com/angular-redux/platform/blob/master/packages/router)
175- [Reduxify your Forms with @angular-redux/form](https://github.com/angular-redux/platform/blob/master/packages/form)
176
177## Resources
178
179- [Using Redux with Angular - JS Toronto Meetup 2016-07-12](https://www.youtube.com/watch?v=s4xr2avwv3s)
180- [Getting started with Redux](https://egghead.io/courses/getting-started-with-redux)
181- [Awesome Redux: Community Resources](https://github.com/xgrommx/awesome-redux)
182
183## In-Depth Usage
184
185`@angular-redux/store` uses an approach to redux based on RxJS Observables to `select` and transform
186data on its way out of the store and into your UI or side-effect handlers. Observables
187are an efficient analogue to `reselect` for the RxJS-heavy Angular world.
188
189Read more here: [Select Pattern](articles/select-pattern.md)
190
191We also have a number of 'cookbooks' for specific Angular topics:
192
193- [Using Angular's Dependency Injector with Action Creators](articles/action-creator-service.md)
194- [Using Angular's Dependency Injector with Middlewares](articles/di-middleware.md)
195- [Managing Side-Effects with redux-observable Epics](articles/epics.md)
196- [Using the Redux DevTools Chrome Extension](articles/redux-dev-tools.md)
197- [@angular-redux/store and ImmutableJS](articles/immutable-js.md)
198- [Strongly Typed Reducers](articles/strongly-typed-reducers.md)
199
200## Hacking on angular-redux/store
201
202Want to hack on angular-redux/store or any of the related packages? Feel free to do so, but please test your changes before making any PRs.
203
204Here's how to do that:
205
2061. Write unit tests. You can check that they work by running
207 `npm test`.
2082. Run the linter. If your editor doesn't do it automatically, do it
209 manually with `npm run lint`.
2103. Test your changes in a 'real world scenario'. We use the [example-app](https://github.com/angular-redux/example-app) for this, using some npm
211 fakery to 'publish the package locally':
212
213- clone the example app (`git clone https://github.com/angular-redux/example-app.git`)
214- generate a 'local package' (`cd` to your `angular-redux/store` clone and run `npm pack`). This will create a `.tgz` file.
215- hook your 'local package' up to your example-app (`cd` to your example-app clone and run `npm install --save /path/to/the/tgz/file/from/above`)
216- run `ng serve --aot`
217
218Please make sure your changes pass Angular's AoT compiler, because it's a bit finicky with TS syntax.