Logo

dev-resources.site

for different kinds of informations.

Component store – perfect for managing local state

Published at
3/1/2024
Categories
angular
rxjs
reactive
frontend
Author
asaf_eliasim
Categories
4 categories in total
angular
open
rxjs
open
reactive
open
frontend
open
Author
12 person written this
asaf_eliasim
open
Component store – perfect for managing local state

For those unfamiliar with @ngrx/component-store, this article serves as an imperative read, urging immediate hands-on practice. Additionally, I hope to uncover new insights and refine existing knowledge for those already familiar with its prowess.

Make yourself a cup of coffee and settle in for a few minutes to read.

πŸ’₯ What is it component store?
The Component Store serves as a tailored state management solution, explicitly designed to handle the state of individual components rather than the global application state, as seen in @ngrx/store. Like other state management solutions, the Component Store enables reading, writing, and handling side effects. Notably, it operates as a push-based service, rendering components reactive. Subscribing to the store allows the view to effortlessly capture immediate changes, amplifying its efficacy.

Within the Component Store architecture, we find four essential components:

βœ… State: The cornerstone of our data management, encompassing all information vital to the component's functionality.

βœ… Selectors: extract specific data from the component's state, enabling convenient and efficient access to relevant information.

βœ… Updaters: modifying the component's state, facilitating seamless updates and modifications.

βœ… Effects: handle side effects, ensuring the integrity and stability of the component's state and behavior.

The state, along with its functionalities, is presented as observables, enabling components to observe and react to state changes.

πŸ’₯ What are the benefits of the component store? What makes it so powerful to use?

😊 Simplicity: It offers a straightforward approach to managing component state, keeping complexity at bay while ensuring efficient state management.
😊 Maintenance: in a direct context to simplicity, it is so comfortable to make some changes and update the service whenever we want.
😊 Performance: Leveraging Agular’s change detection mechanism and observables, Component Store ensures swift state updates and judicious component re-renders, enhancing overall application performance. Moreover, the component state persists as long as the component is active, ensuring efficient resource management.
😊 Encapsulation: Its ability to instantiate multiple instances of the same component, each with its independent state managed within its Component Store, promotes encapsulation and modularity, contributing to a clean and organized codebase.

In summary, the Component Store offers an elegant solution for managing component state, combining simplicity, efficiency, and encapsulation to elevate the development experience.

πŸ’₯ Now that we grasp the capabilities of the Component Store, let's dive into the implementation.

1️⃣ Begin by defining the managed state within the Component Store and initializing it accordingly. (Feel free to expand upon the state structure as needed.)

Image description

This initial state serves as the foundation, ready to scale as needed, accommodating additional details seamlessly.

Omit the providedIn property in the @Injectable decorator, as the Component Store is intended to be provided at the component level.

*While a user type hasn't been created for this example, adhering to best practices dictates that all components should be appropriately typed, and the usage of "any" should be avoided at all costs. *

2️⃣ In our next step, we'll implement selectors to efficiently retrieve data from the state. Leveraging observables, these selectors will ensure a reactive approach, allowing for seamless and elegant handling of data, thereby enhancing both responsiveness and overall user experience.

Image description

3️⃣ Now, let's proceed to define our state updaters, and notice their cleanliness and readability.

Image description

Do not forget to make validations

4️⃣ The final step in setting up the service store involves implementing side-effects. These serve crucial functions such as handling asynchronous operations and managing component state based on external events or data changes.

Image description

The 'setUser' effect, for instance, is responsible for receiving user data and updating the component's state. I've included a 'loading' state as an illustrative example of how we can manage the waiting period for data retrieval from the server.
Another aspect I find particularly satisfying to handle within the store is the 'editButtonStatus' effect. This continuously updates the button's state in the view, determining whether it should be enabled or disabled.
It's worth noting that there's significant potential for customization and manipulation of data streams within these effects using operators. This allows for fine-tuning and tailoring the data flow to precisely fit the requirements of the application.

5️⃣ Now, the last thing, we going to inject the store inside our component and use the selectors and effects.

Image description

When dealing with a child component, simply injecting the store service suffices, eliminating the need for explicit provision, especially when aiming to leverage the same store instance.

reactive Article's
30 articles in total
Favicon
Learning AWS with Localstack and Reactive Kotlin β€” A stamps and coins implementation
Favicon
Concussion Management in Blackfalds: Expert Care at Reactive Clinic
Favicon
Making reactive applications with a Kitten Care Example
Favicon
Reactive Programming applied to Legacy Services β€” A WebFlux example
Favicon
Getting Started with Spring WebFlux
Favicon
Reactive vs. Event-Driven Programming: Weighing the Pros and Cons
Favicon
Building the Neo4j Matrix: Spring Boot, Reactive APIs, and Graph Databases
Favicon
The Article I Took So Long to Write: Concepts about Reactive Programming and RxJS
Favicon
Cloning Reactive Objects in JavaScript
Favicon
Alfama: Fine grained reactive UI library with explicit subscriptions
Favicon
Building Reactive Microservices: A Step-by-Step Guide
Favicon
Unleashing Reactive Programming: Boosting Responsiveness, Resilience, and Scalability
Favicon
Understanding Reactive Contexts in Angular 18
Favicon
WordPress Interactivity API: Detailed Explanation
Favicon
Vue reactivity for impatient devs
Favicon
Reactive vs Servlet Stack
Favicon
May I humbly submit you my front-end framework ?
Favicon
Microservice
Favicon
Component store – perfect for managing local state
Favicon
How Combine works: Operators
Favicon
How Combine works: Subscriptions
Favicon
How Combine works: Publishers
Favicon
Ability to unlearn in Software: Reactive Programming
Favicon
Yes! There’s a Technology that’s Faster than React!
Favicon
Angular Dynamic Form Using XSD
Favicon
Angular Forms: Building Complex Reactive Forms with ngJoyValidators
Favicon
Reactive Programming: a gentle introduction
Favicon
The way we store state globally in react
Favicon
Reactive Backend Applications with Spring Boot, Kotlin and Coroutines (Part 1)
Favicon
Reactive Backend Applications with Spring Boot, Kotlin and Coroutines (Part 2)

Featured ones: