Fetch constant API data to store using NgRx Effects | Angular
NGRX Angular State management (photo credits: https://ngrx.io/)

Fetch constant API data to store using NgRx Effects | Angular

NgRx is?a framework for building reactive applications in Angular and it is deriving state using RxJS and stores a single state and uses actions to express state changes.

NgRx Effects can be used to fetch API data to state variables and In an Angular app, Multiple API requests are being sent from components to fetch updated data from the server but it is not always required especially if data not changing frequently. Overall, It can improve loading time and bandwidth as well.?

This guide demonstrates a method to fetch data using NgRx Effects.

1. Component

component is loaded by the app and it observes state variable through selector. In this example, an object is used to lists the experience of a person and it has been used as an observable so updates to the state variable are being watched asynchronously and displayed in the component.

IExperiance{ workplace: string; role: string; }        

experience.component.ts

No alt text provided for this image

experience.component.html

No alt text provided for this image


2. Initialize on loading for the first time

Have a state action to initialize for the first time to request data when it is needed in the component. The call can be made multiple times in both component and root but the data is fetched from API only when the state variable is empty. It is ideally can be used in ngOnInit() method of the component.

datastore.action.ts

No alt text provided for this image

and make a call when it is needed.

No alt text provided for this image


3. Set state for storing data

Define state variable, selector, reducer and associated action which sets state variable when data is passed on.

If setExperiance(data) is being dispatched, data will be set to state variable "experiance" and it can be accessed from components through the selector "selectExperiance".

datastore.state.ts

No alt text provided for this image

datastore.selector.ts

No alt text provided for this image

datastore.reducer.ts

datastore.reducer.ts

Register reducer in app.module.ts

No alt text provided for this image

datastore.action.ts

No alt text provided for this image


4. NgRx Effect to fetch API data

Next, an effect is created to watch and trigger on initData() method and sets the data using setExperiance(data) if the state variable is empty.

datastore.effects.ts

No alt text provided for this image

In this effect, dispatching of action initData() is being watched and gets the latest value of state variable "experience" using withLatestFrom()

If data is available in "experience" the next action will not be dispatched as a filter check using filter() in place. This will stop unnecessary API calls being made to the server despite the state action is being called multiple times.?

debounceTime(5000) is used to make concurrent API requests delayed by 5 seconds and In case of an API error initData() can be run in a loop but this will make sure all API calls are made after an interval.

Switch Map is being used to get the data from an API service which returns an observable.

datastore.service.ts

No alt text provided for this image

Now, the work is complete and the component can be used wherever required in the app and data will be fetched from the state variable and if data is not available NgRx Effects will request the API calls to fetch the data.

要查看或添加评论,请登录

Zameel Amjed的更多文章

  • Categorize & Display Your Products in a Map - WordPress Plugin

    Categorize & Display Your Products in a Map - WordPress Plugin

    WordPress is a world's most popular website builder tool for creating any kind of website; from a simple blog to a…

    1 条评论
  • Website for as low as 10USD

    Website for as low as 10USD

    Today we are living in the digital world so a website is a must for any nature of business. Let it be big or small, if…

  • WannaCry: Prevention is Better than Cure

    WannaCry: Prevention is Better than Cure

    Ransomware is a type of malicious software that steals or encode data to an unreadable format and after an outbreak the…

  • An Evolution of a Call Centre

    An Evolution of a Call Centre

    The Minimalist The management was challenged when most CRM vendors could not comply with our special set of…

  • Rapid Hybrid App Development

    Rapid Hybrid App Development

    When there was a requirement to develop an app for the bookprices.lk, my ultimate choice was to develop the app using…

社区洞察

其他会员也浏览了