Angular State Management With NGRX
Angular State Management With NGRX
State management is the process of managing the states of user controls. It helps developers build large-scale applications with
sustaining high application performance.
When it comes to Angular, NgRx and NGXS are the two most-used libraries for state management, and they have some unique fe
In this article, I will introduce NgRx and walk you through the steps of creating a simple Angular application using it.
What is NgRx?
NgRx Website
NgRx is inspired by Redux and helps developers simplify the application’s state in objects by enforcing a unidirectional data flow.
Reducers: Reducers are responsible for handling the transition from one state to another.
Effects: Effects are the results of actions. Also, they can be used to listen for particular action types and run when the
Although this process seems complex to implement, it is useful for data communication when the application grows.
// NPM
npm install @ngrx/store --save
// Yarn
yarn add @ngrx/store
// Angular CLI
ng add @ngrx/store@latest
As a first step, you need to create an interface for articles. Let’s name the model article.model.ts.
In the above action file, we defined an action as a constant using enum. Then, we implemented the AddArticleAction class from
parameters: a type and an optional payload.
Now, we need to create a reducer to help us in state transitions. So, inside the reducers directory, create a course.reducer.ts file
In the above reducer, we have created an initial state for the Article interface and a reducer function named ArticleReducer Whi
parameters. If the action type is ADD_ITEM, it will return the state and the payload. Otherwise, it will only return the state.
For the final step of store creation, we need to create another model to keep all application states in a single place. Here, we hav
like this:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
StoreModule.forRoot({
course: CourseReducer,
}),
],
providers: [],
bootstrap: [AppComponent]
})
Each and every property of Syncfusion Angular components are completely document
for easy access.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
ngOnInit(): void {
this.articles$ = this.store.select((store) => store.article);
}
addArticle(form: NgForm) {
this.store.dispatch(
new AddArticleAction(form.value)
);
form.reset();
}
The above code shows the app.component.ts file of our example. There, we have set articles$ to a type of observable and subsc
addArticle() function is responsible for dispatching new articles to the store.
Now, we need to modify the app.component.html file to display and create articles.
<section>
<div class="container">
<div class="row" style="margin-top: 5%;">
<div class="col-md-12" style="text-align: center; margin: 5%;">
<h2>My Articles</h2>
</div>
<div class="col-md-6">
<div class="card p-4 shadow-sm">
<form #myform="ngForm" (ngSubmit)="addArticle(myform)">
<div class="form-group">
<label for="name">Article Id</label>
<input
type="text"
class="form-control"
ngModel
name="id"
id="id"
aria-describedby="identity"
required
/>
</div>
<div class="form-group">
<label for="title">Title</label>
<input
type="text"
class="form-control"
ngModel
name="title"
id="title"
aria-describedby="title"
/>
</div>
<div class="form-group">
<label for="author">Author</label>
<input
type="text"
class="form-control"
ngModel
name="author"
id="author"
/>
</div>
<div class="form-group">
<label for="publisher">Publisher</label>
<input
type="text"
class="form-control"
ngModel
name="publisher"
id="publisher"
/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<div class="col-md-6">
<ul class="list-group">
<li class="list-group-item list-group-item-primary" *ngFor="let article of articles$ | async">
{{article.title}} <b>{{article.author}}</b>
</li>
</ul>
</div>
</div>
</div>
</section>
That’s all there is to it! We have successfully configured an NgRx store with an Angular application.
GitHub reference
For more details, you can find the Angular State Management with NgRx demo on GitHub.
See the possibilities for yourself with live demos of Syncfusion Angular components.
Conclusion
NgRx is one of the most-used libraries for state management in Angular applications. I hope this article gives you a head start in
using NgRx.
Syncfusion’s Angular UI component library is the only suite you will ever need to build an application, containing a wide range of
and responsive UI components in a single package.
For existing customers, the newest Essential Studio version is available for download from the License and Downloads page. If yo
can try our 30-day free trial to check out the available features. Also, check out our demos on GitHub.
You can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!
Related blogs
Syncfusion Essential Studio 2024 Volume 1 Is Here!
Tags:
CHAMEERA DULANGA
Software engineer with more than 3 years of working experience in React, Angula
Recognized as an AWS Community Builder for 2022. Tech Blogger since 2019 with
More Posts
Popular Now
10 JavaScript Naming Conventions Every Developer Should Know
Email Address
Subscribe