Angular
functional style
Vasyl Sydorivskyi – Innovecs
Why functional?
• Easy to test
• Easy to maintain
• Pure functions
Simple app
• @input and @output
decorators to pass
information between
parent and child
components
App
component
Component 1
Complex app
• A lot of inputs and outputs
• State sharing services
App
component
Component 1 Component 2
Component 3 Component 4 Component 5 Component 6
State management
• Single store
• Read-only state
• Pure function reducers
@ngrx/store
REDUCER
COMPONENTS
Actions
State
Action
interface Action {
type: string;
payload?: any;
}
Reducer
interface Reducer<State> {
(state: State, action: Action): State;
}
Store
class Store<T> extends Observable<T> {
select((state: T) => R): Observable<R>
dispatch(action: Action): void;
}
Recap
• Actions represent any event in application
• Reducers "reduce" actions into new state
• State could be shared with any component
@ngrx/effects
• Any interaction with outside world
• HTTP requests, websockets, etc.
• Components are free from side-effects
REDUCER
COMPONENTS
Actions
State
Side-effects
Actions
Actions
• Actions is an observable of all actions
• ofType is an operator that filters specific actions
• Can produce new actions in response
class Actions extends Observable<Action> {
ofType(...keys: string[]): Actions;
}
@ngrx/dev-tools
• Enables Redux Devtools
Extension
• Time traveling
Wrapping up
• Application state is observable and immutable
• It has shared state service
• Side effects are isolated from components
DEMO
Thank you!
Useful links
• https://gist.github.com/btroncone/
a6e4347326749f938510#introduction
• https://www.youtube.com/watch?
v=cyaAhXHhxgk

Vasyl Sydorivskiy - Angular Functional Style