diff --git a/projects/ngrx.io/content/guide/store/index.md b/projects/ngrx.io/content/guide/store/index.md index 6dd1dc72d5..2012b22bdb 100644 --- a/projects/ngrx.io/content/guide/store/index.md +++ b/projects/ngrx.io/content/guide/store/index.md @@ -39,27 +39,79 @@ The following tutorial shows you how to manage the state of a counter, and how t 2. Right click on the `app` folder in StackBlitz and create a new file named `counter.actions.ts` to describe the counter actions to increment, decrement, and reset its value. - + + +```ts +import { createAction } from '@ngrx/store'; + +export const increment = createAction('[Counter Component] Increment'); +export const decrement = createAction('[Counter Component] Decrement'); +export const reset = createAction('[Counter Component] Reset'); +``` + 3. Define a reducer function to handle changes in the counter value based on the provided actions. - + + +```ts +import { createReducer, on } from '@ngrx/store'; +import { increment, decrement, reset } from './counter.actions'; + +export const initialState = 0; + +export const counterReducer = createReducer( + initialState, + on(increment, (state) => state + 1), + on(decrement, (state) => state - 1), + on(reset, (state) => 0) +); +``` + 4. Import the `StoreModule` from `@ngrx/store` and the `counter.reducer` file. - + + +```ts +import { StoreModule } from '@ngrx/store'; +import { counterReducer } from './counter.reducer'; +``` + 5. Add the `StoreModule.forRoot` function in the `imports` array of your `AppModule` with an object containing the `count` and the `counterReducer` that manages the state of the counter. The `StoreModule.forRoot()` method registers the global providers needed to access the `Store` throughout your application. - + + +```ts +import { BrowserModule } from '@angular/platform-browser'; +import { NgModule } from '@angular/core'; + +import { AppComponent } from './app.component'; + +import { StoreModule } from '@ngrx/store'; +import { counterReducer } from './counter.reducer'; + +@NgModule({ + declarations: [AppComponent], + imports: [BrowserModule, StoreModule.forRoot({ count: counterReducer })], + providers: [], + bootstrap: [AppComponent], +}) +export class AppModule {} +``` + 6. Create a new file called `my-counter.component.ts` in a folder named `my-counter` within the `app` folder that will define a new component called `MyCounterComponent`. This component will render buttons that allow the user to change the count state. Also, create the `my-counter.component.html` file within this same folder. - + + +```ts + import { Component } from '@angular/core'; import { Observable } from 'rxjs'; @@ -86,30 +138,92 @@ export class MyCounterComponent { // TODO: Dispatch a reset action } } +``` + - <button (click)="increment()">Increment</button> - <div>Current Count: {{ count$ | async }}</div> +```html + - <button (click)="decrement()">Decrement</button> +
Current Count: {{ count$ | async }}
- <button (click)="reset()">Reset Counter</button> -
+ + +``` + +
7. Add the new component to your AppModule's declarations and declare it in the template: - + + +```html + +``` + - + + +```ts +import { BrowserModule } from '@angular/platform-browser'; +import { NgModule } from '@angular/core'; + +import { AppComponent } from './app.component'; + +import { StoreModule } from '@ngrx/store'; +import { counterReducer } from './counter.reducer'; +import { MyCounterComponent } from './my-counter/my-counter.component'; + +@NgModule({ + declarations: [AppComponent, MyCounterComponent], + imports: [BrowserModule, StoreModule.forRoot({ count: counterReducer })], + providers: [], + bootstrap: [AppComponent], +}) +export class AppModule {} +``` + 8. Inject the store into `MyCounterComponent` and connect the `count$` stream to the store's `count` state. Implement the `increment`, `decrement`, and `reset` methods by dispatching actions to the store. - + + +```ts +import { Component } from '@angular/core'; +import { Store } from '@ngrx/store'; +import { Observable } from 'rxjs'; +import { increment, decrement, reset } from '../counter.actions'; + +@Component({ + selector: 'app-my-counter', + templateUrl: './my-counter.component.html', +}) +export class MyCounterComponent { + count$: Observable; + + constructor(private store: Store<{ count: number }>) { + this.count$ = store.select('count'); + } + + increment() { + this.store.dispatch(increment()); + } + + decrement() { + this.store.dispatch(decrement()); + } + + reset() { + this.store.dispatch(reset()); + } +} +``` + And that's it! Click the increment, decrement, and reset buttons to change the state of the counter.