Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Understanding ProvideStore() in Ngrx/Store

12/26/2016

0 Comments

 
This post is meant to explain some of the magic behind the line of initialization code for Ngrx/Store that you put in the imports block of the NgModule. Working with provideStore is key to understanding how to setup your project wiht Ngrx, so let's get into it!

StoreModule.ProvideStore

In the app.module file of your application you should have something like this:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Now, in order to let our application know that we want to use Ngrx we just need to add one line to our imports array (not including the import statement at the top of the file bringing in StoreModule from "@ngrx/store"). The line we'll add looks something like this:
 StoreModule.provideStore({myReducer}),
The providerStore function takes an object of one or more reducers (functions of type ActionReducer). ProvideStore also takes an additional optional argument which is an initial state to use. I tend to leave this argument off and just provide an initial state to each reducer. Remember, our reducer function looks something like this:
export const myReducer: ActionReducer<AppState> =
  (state = intitialState, action: Action) => {
    ...
  }
In the code above we're exporting a constant myReducer, which is of type ActionReducer, and I'm telling it that this ActionReducer takes a State defined by my interface AppState which looks like this: 
export interface AppState {
  user:User,
  bullets: Bullet[]
};
Notice that this AppState interface is built from two other custom types: User and Bullet. I've also defined interfaces for them, and they look like this:
export interface User {
  name: string
  xPos: number
  yPos: number
};

export interface Bullet {
  xPos: number,
  yPos: number,
  shotBy: string
};
And I'm also defining an initial state which looks like this:
export const intitialState: AppState = {
  user: {name: 'Jim',
    xPos: 0,
    yPos: 0},
  bullets: []
};

That's It!

Well, that's all there really is to setting up Ngrx and using provideStore. Now in your smart component or service you can inject store, dispatch events to your reducers, and subscribe to slices of the state like this:
constructor(private http:Http, private store:Store<GlobalState>) {

this.store.select(s => s.myReducer.user)
  .subscribe( (data:any)=> {
    console.log('app component got reducer\'s state: ' + data);
  });

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

}
And remember, the global state is created from the state of each of our reducers that we pass into provideStore. Since we're only providing one reducer in this case our GlobalState would look like this:
export interface GlobalState {
  myReducer: AppState
}
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletter called,
    "The Triple Gainers"?

    Sign up here!

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021