Can triggering an ngrx effect lead to memory leakage?

Currently, I am delving into the world of NgRx and grappling with a concept that has been puzzling me. As I create an Effect and dispatch an action, the createEffect function comes into play. What throws me off is the dispatch configuration within createEffect, which according to the NgRx documentation, has a boolean parameter:

The config controls whether the action emitted by the effect is sent to the store. If set to false, the effect does not need to return type Observable<Action>.

If we consider the default setting where dispatch is true, it triggers an action, initiating the execution of createEffect(), consequently leading to another action emission. With dispatch still being true, this sets off an ongoing recursive loop.

Let's examine this through a reproducible example:

Sample Action:

export const incrementByN = createAction('[Counter Component] Increment With N', props<{value: number}>())

Sample Reducer:

export let counterReducer = (state = initialState, action: CounterActions | Action): number => {
    if (action.type === '[Counter Component] Increment') {
        return state + (action as CounterActions).value
    }
 
    return state
}

Sample Effect:

@Injectable()
export class CoutnerEffect {
    saveCount = createEffect(() => {
        return this.actions$.pipe(
            ofType(incrementByN, decrement), 
            tap(action => {
                console.log(action)
            })
        )
    })
    constructor(private actions$: Actions) {}
}

However, I suspect there might be a memory leak issue when dispatch is left at its default value of true.

Answer №1

An impact is utilized to manage the repercussions of an application, such as initiating a request to the backend.

A common impact monitors an action, handles repercussions, and triggers a new action based on the outcome. For instance, it may trigger an action containing the results of a backend request or an error action with the specific error details.

If you do not assign the action to a new one (like in the given example), the impact will continue to be invoked repeatedly since it receives the same action. This can be intentional behavior in certain situations, like polling for data.

If you wish to avoid this scenario, either map the incoming action to a different one, or utilize the dispatch: false setting to prevent the action from being triggered at the end of the effect.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating the syntax of React with Typescript - Feeling lost

As I embark on my journey with Typescript/React, I find myself trying to decode the meanings behind it all. Coming from a C# background, this new environment presents a unique challenge for me. Despite going through several tutorials, I still find myself p ...

Exploring the Ways to Determine Array Type in Typescript Generics

I'm working with a method that looks like this: public select(fieldName: keyof TType) In this scenario, TType can potentially be an array type. If fieldName is called with a type of User[], I want to access the properties of User instead of the defa ...

Make a decision on whether to use a Typescript type or interface based on an external

My goal is to select a TS interface or type based on an external condition rather than the input. This external condition could be a feature toggle, for example. Allow me to elaborate: type NotEmptyName = string; type EmptyableName = string | null; cons ...

Is there a way to extract the current view date information from fullcalendar using Angular?

When working with HTML code... <full-calendar defaultView="dayGridMonth" [plugins]="calendarPlugins" #calendar></fullcalendar> I am wondering how to extract the date information from the #calendar element. I attempted to consult the fullcal ...

Utilizing props in React results in the introduction of an array

One of my components sends an array of movies to a child component: const films: IMovie[] = data.movies; console.log(films); return ( <React.Fragment> <DashboardMovieOverviewMenu /> { films.length > 0 ? <MovieOverview movies={f ...

Set up Nginx to host Angular static files and act as a reverse proxy for express

Recently, I've started using Nginx for serving static Angular files and proxying to a backend Express server. Both the frontend (with Nginx) and backend are dockerized. My frontend consists of a button that makes a request to fetch data from Express. ...

Two tags attached to four hypertext links

Within my HTML code, I have hyperlinks present on every line. However, I am seeking to eliminate these hyperlinks specifically from "your previous balance" and "your new balance".https://i.sstatic.net/ekVGT.png In the following HTML snippet: <tr *ngFor ...

Security concern regarding XSRF in Spring and Angular 5

For my current project, I am using Spring as the backend (generated with microservices with Jhipster) and Angular5 as the frontend. On the server side, CSRF security is added (it was enabled by default when we created microservices with Jhipster). Workin ...

Efficiently handling heavy components in Angular using ngFor

Encountered an issue: I have an array containing chart configurations that need to be displayed. Currently, I am iterating through the array and rendering the charts as shown below: <ng-container *ngFor="let config of configs; trackBy: getId"& ...

Incorporating and modifying a component's aesthetics using styled-components: A comprehensive guide

My OverviewItem component has 2 props and is a styled-component. I want to change just one style on this component, which can be done by using the technique of styling any component. Does creating a wrapper component remain the only option for sharing st ...

Apollo GraphQL has initiated the detection of a new subscription

My approach involves utilizing graphql-ws for subscribing to GraphQL events. I rely on the Observable interface to listen to these events. Although I can use the error callback to identify when a subscription fails to start, it is challenging to determine ...

Using React to make an API call without utilizing hooks

Hello, I am currently working on developing a webpart using SharePoint and React. However, I am facing some issues with fetching data from a simple API. export default class Testing100 extends React.Component<ITesting100Props, {}> { constructor(p ...

Tips on concealing the scrollbar only when a particular element is visible

I inserted the following code into my index.html file: <head> <style> body::-webkit-scrollbar { display: none; } </style> </head> This code successfully hides the scrollbar. My goal is to only h ...

Tips for implementing server-side pagination using NestJS

Working with a MEVN stack that includes Nestjs, MongoDB (mongoose), I am currently tackling the task of setting up server-side pagination. I've decided to utilize mongoose-aggregate-paginate-v2 for this purpose, but so far, I haven't been able to ...

strategies for chaining together multiple observables with varying data types and operations

Hey everyone! I'm facing a situation where I have a form with multiple select types, and the options for these inputs are coming from an API. I then take the data emitted from the HTTP request observable and feed it into my FormGroup, and everything i ...

Patterns for Spring Boot backend and Angular frontend designor Strategies for designing a Spring

I have developed an application that utilizes Spring Boot for its back end and Angular for the front end, connected through APIs. The Spring Boot components include: Dao Controller Entity Service and other classes. Upon studying further, it appears that ...

I noticed an excess of white space on the right side of my Angular website

Check out my website here. Everything seems to be functioning correctly, however, there is an issue with scrolling on mobile. It should only scroll up and down, not left and right! I have noticed a strange white space on the right side of my site when view ...

Adjust the size of the mat-expansion indicator to your desired height and width

Trying to modify the width and height of the mat indicator has been a bit challenging. Despite following suggestions from other similar questions, such as adjusting the border width and padding, I am still unable to see the changes reflect in my CSS file ...

Typescript: issue with type guard functionality not functioning properly

type VEvent = { type: "VEVENT"; summary: string; }; type VCalendar = { type: "VCALENDAR"; }; const data: Record<string, VEvent | VCalendar> = (...); array.map((id) => { return { id, title: dat ...

Identifying Shifts in Objects Using Angular 5

Is there a way to detect changes in an object connected to a large form? My goal is to display save/cancel buttons at the bottom of the page whenever a user makes changes to the input. One approach I considered was creating a copy of the object and using ...