An Introduction to NgRx/Store in Angular Application
Introduction
When building a medium or a large scale Single Page Application (SPA), stage management is matters. This article discussing the following very shortly.
- State
- Ngrx/Store
- Single Source of Truth
- Actions
- Reducers
- Selectors
- Effects
In order to get a better understanding of NgRx, those are very important areas that should be clear.
Overview
State
The application state includes data(that is received via API calls), user inputs, variables, and so on.
In the modern javaScript frameworks such as Angular, every component has its own state. So the application states can be anywhere in the components.
In Angular, @Input() and @Output() decorators are using to pass data between parent and child components. And can use other different ways to share data between components that are siblings. The following diagram represents how data passing from components to components.
Passing data in this way is not a big deal when the application is small. But imagine the medium or large applications that have hundreds of thousands of components. Passing information in this way will definitely become complex and it will become hard to identify and fix any breaking changes in the states.
So to solve this problem the Ngrx starts smiling.
NgRx
NgRx is an open-source state management library.
It is inspired by Redux. It is a state management library for JavaScript applications.
NgRx provides a way to maintain the Angular Application’s state in a Single Source Of Truth.
Single Source Of Truth
This means that the state of your whole application is stored in an object tree within a single store.
Here store is the collection of application states. The application’s components can get reference via dependency injection.
So with the single store, all information in the application flows in one way.
The store is immutable. Cannot change the store directly. In order to make changes in this store, there is a feature called Actions.
Actions
Actions change the state of the store by using reducers. According to the dispatched action, the store executes the ideal operation.
Mainly action has two attributes
- Type
- Payload
The payload is an optional attribute. That takes additional metadata. The following code snippet represents an action.
Reducers
Reducer changing the state and returning the new state. And also it keeps the store immutable. Reducers take the current state and action as parameters.
Selectors
Selectors are pure function that receives the part of the application’s states.
Pure function
If the function is pure, if every time calls that function and gives the same argument, it always returns the same result.
If the function is not pure, if every time calls that function and gives the same argument, it will return a different result.
Effects
Effects allow us to perform additional operations. For example, let’s say the action is dispatched to get the customers. Effects allow us to define additional tasks like what should do when the load is a success, what should do when an error occurred.
So the NgRx effects used to handle the side effects.
So these are the main concepts in NgRx. The following diagram represents the overall general flow (State Management Lifecycle) of the application state in NgRx.
Conclusion
The NgRx provides global state management for your Angular Application. When you are working with the medium/large scale of project and state management is becoming very complex, The NgRx is the right choice for you.
Takeaways
- State
- Ngrx/Store
- Single Source of Truth
- Actions
- Reducers
- Selectors
- Effects
Thank you
Thank you very much for spending your time. I hope this article will give you an idea about NgRx and how it works. Thanks