NGRX is a popular library for managing the state of an Angular application. It is built on top of the RxJS library and uses the principles of reactive programming to make it easier to handle complex state management tasks.
One of the main advantages of NGRX over plain RxJS is that it provides a consistent and well-structured way to manage the state of an application. With NGRX, the state of the application is managed in a centralized store, which makes it easy to keep track of all the state changes and handle them in a predictable way.
NGRX also provides a set of powerful tools for managing the state of an application, such as actions, reducers, and effects. Actions are used to describe the changes to the state, reducers are used to update the state based on the actions, and effects are used to handle side-effects, such as making an API call or navigating to a different page.
Another advantage of NGRX is that it is designed to work seamlessly with Angular. It uses Angular's change detection mechanism to automatically update the view when the state changes, and it also integrates with Angular's dependency injection system, which makes it easy to manage the state throughout the application.
Additionally, NGRX provides a set of powerful tools for debugging, testing, and performance optimization, such as the ability to log state changes, time-travel debugging, and memoized selectors.
Using plain RxJS:
import { BehaviorSubject } from 'rxjs';
const counterSubject = new BehaviorSubject(0);
function increment() {
const currentCount = counterSubject.getValue();
counterSubject.next(currentCount + 1);
}
function decrement() {
const currentCount = counterSubject.getValue();
counterSubject.next(currentCount - 1);
}
counterSubject.subscribe(count => {
console.log(count);
});
In this example, a BehaviorSubject is used to manage the state of the counter. The BehaviorSubject is initialized with an initial value of 0. The increment and decrement functions are used to update the state by calling the next method on the BehaviorSubject and passing the new count.
The subscribe method is used to subscribe to the counterSubject and log the current count to the console whenever it changes.
Using NGRX:
import { StoreModule, Store } from '@ngrx/store';
import { counterReducer } from './counter.reducer';
@NgModule({
imports: [
StoreModule.forRoot({ count: counterReducer })
]
})
export class AppModule { }
export const increment = createAction('[Counter] Increment');
export const decrement = createAction('[Counter] Decrement');
@Injectable()
export class CounterService {
count$ = this.store.pipe(select('count'));
constructor(private store: Store) { }
increment() {
this.store.dispatch(increment());
}
decrement() {
this.store.dispatch(decrement());
}
}
In this example, NGRX is used to manage the state of the counter. The StoreModule is imported from the @ngrx/store library and configured with a root reducer that handles the counter state.
Actions are defined using the createAction method, increment and decrement actions are used to update the state. The store is injected into a service, which has a count$ property that selects the count from the store and makes it available for subscription.
The increment and decrement methods dispatch the corresponding actions to the store, which causes the state to update.
With this example, you can see that NGRX provides a more structured way to manage the state of an application. It enforces a clear separation of concerns between the state, the actions that update the state, and the reducers that handle the actions. It also provides a powerful set of tools for debugging, testing, and performance optimization, such as the ability to log state changes, time-travel debugging, and memoized selectors.
Additionally, NGRX provides a built-in mechanism for handling async actions with the help of the @Effect decorator, which makes it easy to handle and manage async code in your application.
In summary, NGRX provides a powerful and well-structured way to manage the state of an Angular application, and it provides a set of powerful tools for managing complex state management tasks. It is built on