• This repository has been archived on 10/Jan/2018
  • Stars
    star
    3,908
  • Rank 11,200 (Top 0.3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RxJS powered state management for Angular applications, inspired by Redux

This repository is for version 2.x of @ngrx/store.

Click here for the latest version (4.x)


@ngrx/store

RxJS powered state management for Angular applications, inspired by Redux

Join the chat at https://gitter.im/ngrx/store CircleCI Status for ngrx/store npm version

@ngrx/store is a controlled state container designed to help write performant, consistent applications on top of Angular. Core tenets:

  • State is a single immutable data structure
  • Actions describe state changes
  • Pure functions called reducers take the previous state and the next action to compute the new state
  • State accessed with the Store, an observable of state and an observer of actions

These core principles enable building components that can use the OnPush change detection strategy giving you intelligent, performant change detection throughout your application.

Installation

Install @ngrx/core and @ngrx/store from npm:

npm install @ngrx/core @ngrx/[email protected] --save

Optional packages:

Examples

  • Official @ngrx/example-app is an officially maintained example application showcasing best practices for @ngrx projects, including @ngrx/store and @ngrx/effects
  • angular-webpack2-starter is a complete Webpack 2 starter with built-in support for @ngrx. Includes Ahead-of-Time (AOT) compilation, hot module reloading (HMR), devtools, and server-side rendering.

Introduction

Setup

Create a reducer function for each data type you have in your application. The combination of these reducers will make up your application state:

// counter.ts
import { ActionReducer, Action } from '@ngrx/store';

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET = 'RESET';

export function counterReducer(state: number = 0, action: Action) {
	switch (action.type) {
		case INCREMENT:
			return state + 1;

		case DECREMENT:
			return state - 1;

		case RESET:
			return 0;

		default:
			return state;
	}
}

In your app's main module, import those reducers and use the StoreModule.provideStore(reducers) function to provide them to Angular's injector:

import { NgModule } from '@angular/core'
import { StoreModule } from '@ngrx/store';
import { counterReducer } from './counter';

@NgModule({
  imports: [
    BrowserModule,
    StoreModule.provideStore({ counter: counterReducer })
  ]
})
export class AppModule {}

You can then inject the Store service into your components and services. Use store.select to select slice(s) of state:

import { Store } from '@ngrx/store';
import { INCREMENT, DECREMENT, RESET } from './counter';

interface AppState {
  counter: number;
}

@Component({
	selector: 'my-app',
	template: `
		<button (click)="increment()">Increment</button>
		<div>Current Count: {{ counter | async }}</div>
		<button (click)="decrement()">Decrement</button>

		<button (click)="reset()">Reset Counter</button>
	`
})
class MyAppComponent {
	counter: Observable<number>;

	constructor(private store: Store<AppState>){
		this.counter = store.select('counter');
	}

	increment(){
		this.store.dispatch({ type: INCREMENT });
	}

	decrement(){
		this.store.dispatch({ type: DECREMENT });
	}

	reset(){
		this.store.dispatch({ type: RESET });
	}
}

Contributing

Please read contributing guidelines here.

More Repositories

1

platform

Reactive State for Angular
TypeScript
7,986
star
2

example-app

Example app showcasing the ngrx platform
TypeScript
1,362
star
3

effects

Side effect model for @ngrx/store
TypeScript
532
star
4

store-devtools

Developer Tools for @ngrx/store
TypeScript
326
star
5

router

Reactive Router for Angular
TypeScript
269
star
6

db

RxJS powered IndexedDB for Angular apps
TypeScript
186
star
7

router-store

Bindings to connect the Angular Router to @ngrx/store
TypeScript
182
star
8

ngrx.github.io

Reactive Extensions for Angular
HTML
114
star
9

notify

Web Notifications Powered by RxJS for Angular
TypeScript
101
star
10

core

Core functionality for the ngrx platform
TypeScript
72
star
11

store-log-monitor

Log Monitor for @ngrx/store-devtools and Angular
TypeScript
63
star
12

signal-store-workshop

Source Code for NgRx SignalStore Workshop
TypeScript
29
star
13

store-builds

@ngrx/store build artifacts
JavaScript
16
star
14

store-devtools-builds

@ngrx/store-devtools build artifacts
JavaScript
10
star
15

entity-builds

@ngrx/entity build artifacts
JavaScript
8
star
16

component-store-builds

@ngrx/component-store build artifacts
JavaScript
6
star
17

angular

TypeScript
5
star
18

signals-builds

JavaScript
4
star
19

schematics-builds

@ngrx/schematics build artifacts
4
star
20

ngrx-io-builds

ngrx.io builds
HTML
4
star
21

router-store-builds

@ngrx/router-store build artifacts
JavaScript
2
star
22

ngrx-io-previews

ngrx.io preview builds
HTML
1
star
23

ngrx-modules-workshop

NgRx Global store workshop - module-based application
TypeScript
1
star
24

signal-store-starter

Stackblitz Project for the SignalStore
TypeScript
1
star
25

ngrx-standalone-workshop

TypeScript
1
star
26

data-builds

@ngrx/data build artifacts
JavaScript
1
star
27

effects-builds

@ngrx/effects build artifacts
JavaScript
1
star