๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ก๐—ด๐—ฅ๐˜… ๐—˜๐—ณ๐—ณ๐—ฒ๐—ฐ๐˜๐˜€ ๐—ถ๐—ป ๐—”๐—ป๐—ด๐˜‚๐—น๐—ฎ๐—ฟ ๐Ÿญ๐Ÿด: ๐—” ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ ๐—•๐—ผ๐—ผ๐˜€๐˜๐—ถ๐—ป๐—ด ๐—š๐˜‚๐—ถ๐—ฑ๐—ฒ

Stephen Oloto
2 min readFeb 4, 2025
lazy-loading-ngrx-effects-stephen-banner

NgRx is a powerful state management library for Angular applications, but as your app scales, eagerly loaded effects can slow down performance. Lazy loading NgRx effects in Angular 18 helps optimize resource usage and improve app efficiency. In this guide, weโ€™ll walk through the steps to set up lazy-loaded effects in your Angular application.

๐—ช๐—ต๐˜† ๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ ๐—˜๐—ณ๐—ณ๐—ฒ๐—ฐ๐˜๐˜€?

By default, NgRx effects are eagerly loaded, meaning they are initialized when the application starts. However, not all parts of an application need to load immediately. Lazy loading effects ensures that specific effects only initialize when their corresponding feature modules or routes are loaded, leading to:

  1. โšก Reduce initial load time โ€” Your app starts faster by skipping unnecessary effects.
  2. ๐Ÿ“‰ Optimize resource usage โ€” Effects wonโ€™t take up memory until required.
  3. ๐Ÿ”ฅ Enhance user experience โ€” Users get a smoother, more responsive app.

๐—ฆ๐—ฒ๐˜๐˜๐—ถ๐—ป๐—ด ๐—จ๐—ฝ ๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด ๐—ณ๐—ผ๐—ฟ ๐—ก๐—ด๐—ฅ๐˜… ๐—˜๐—ณ๐—ณ๐—ฒ๐—ฐ๐˜๐˜€

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿญ: Install NgRx (If Not Installed)

If you havenโ€™t already set up NgRx in your project, install the required dependencies:

๐™ฃ๐™ฅ๐™ข ๐™ž๐™ฃ๐™จ๐™ฉ๐™–๐™ก๐™ก @๐™ฃ๐™œ๐™ง๐™ญ/๐™จ๐™ฉ๐™ค๐™ง๐™š @๐™ฃ๐™œ๐™ง๐™ญ/๐™š๐™›๐™›๐™š๐™˜๐™ฉ๐™จ

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฎ: Define a Feature State and Actions

Letโ€™s assume we have a Products route that needs to be lazily loaded. First, define the actions for handling API requests.

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฏ: Create the Feature Effects

Next, define the ProductsEffects class to handle API calls.

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฐ: Import the store and app reducers into the Application config

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฑ: Lazy Load the Effects in Routing

Assuming you have a Main component route setup that leads to your product routes, this is where we configure Lazy loading in the routing.

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฒ: Verify Lazy Loading

To ensure that the NgRx effects are lazily loaded, open the Network tab in Chrome DevTools and navigate to the Products page. You should see a request to load the chunk dynamically only when it is needed.

Also from the Redux DevTools, the effects are provided at the point of routing, if it were not the API calls from the Network tab would not be initiated.

Effect is not loaded yet, hence API call from the ProductsEffect is not made
Products Effect is only provided to be called when routed to the Products Component

๐—–๐—ผ๐—ป๐—ฐ๐—น๐˜‚๐˜€๐—ถ๐—ผ๐—ป

Lazy-loading NgRx effects is a game-changer for Angular apps! It improves performance, reduces unnecessary processing, and ensures your app remains smooth and efficient.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Stephen Oloto
Stephen Oloto

Written by Stephen Oloto

An Innovative Software Engineer with 5+ years' experience designing and launching responsive websites, fintech solutions, and e-commerce platforms.

No responses yet

Write a response

More from Stephen Oloto