Introduction

This week I’ve been playing around (again) with @ngrx, its store, effects, and go on, trying to integrating to a version of Tour of Heroes. It’s been more than a year since the last time I play with it, and I have to say, I’m becoming a more productive developer, and this has also evolve to become a better platform. The fact that observables now use piped operators, makes everything easier to understand.

I been watching some tutorials, and been reading a lot about the changes introduced with recent versions of the store, and I don’t know. I change the application state in two days. You may think that is a lot of time, but I didn’t know how to use effects, and also I wanted to follow the Angular Style Guide on this one, and the example app of @ngrx does not follow it, and most of the redux examples are made, well, for a React project.

I had my thoughts about the structure of an Angular application, but they were heavily influenced by the structure of React applications. The current Angular Code Style have change my mind about, but this is for another post.

What I want to talk in this post, is about the recommended way to declare a state.

1
2
3
4
5
6
7
export interface AppState {
counter: number;
}

export const initialAppState: AppState {
counter: 3,
}

Why? Ok, here is why. It IS recommended that you use a POJO for declaring the state, and the actions. But, does it really hurt to declare an immutable Typescript class to declare the State?

Classy

1
2
3
export class AppState {
public readonly counter: number = 3;
}

When this is set in the store configuration, the state initializes as a POJO as well, so there is no side effects going around here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { StoreModule } from '@ngrx/store';

export class AppState {
public readonly counter: number = 3;
}

const reducers = (state = new AppState(), action) => state; // Best reducer ever!


@NgModule({
imports: [
StoreModule.forRoot(reducers, {
initialState: new AppState(),
})
]
})
export class AppStoreModule {}

I think this is clearer. You make sure that you set all the required properties, and only need to call the constructor when ever you need. And, you can use the AppState as a interface, wherever you want.

Do you think this is a good thing o a bad thing? Why? I would like to read your comments.