Lädt...


🔧 Make your Angular NgRx reducers an eye candy 🍬 using this fantastic library


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

NgRx is a popular state management library for Angular applications. It provides a centralized store for storing the application’s state and a set of tools for updating the state in a predictable manner. NgRx also provides a powerful set of reducers, which are functions that specify how the state should change in response to an action.

One of the core concepts in NGRX is immutability, which means that the state should never be modified directly. Instead, when an action is dispatched, the reducer creates a new copy of the state with the necessary changes. This helps ensure that the state remains predictable and that it can be easily tested and debugged.

However, creating a new copy of the state can be quite a pain, especially for complex state structures. To address this issue, ngrx-immer was created, which is a library that provides an easier way to handle immutability in NgRx reducers.

Immer

NgRx on method vs immerOn method

Consider a state with a list of users, where each user has a name and an age. We want to add a new user to the list, update the name of an existing user, and delete a user from the list.

Using the on method, we would write the following NGRX reducer:

import { createReducer, on } from '@ngrx/store';
import { addUser, updateUser, deleteUser } from './user.actions';

export interface User {
  name: string;
  age: number;
}

export const initialState: User[] = [];

export const _userReducer = createReducer(
  initialState,
  on(addUser, (state, { user }) => [...state, user]),
  on(updateUser, (state, { user, name }) =>
    state.map(u => (u.name === user.name ? { ...u, name } : u))
  ),
  on(deleteUser, (state, { user }) =>
    state.filter(u => u.name !== user.name)
  )
);

Using the immerOn method from ngrx-immer, we would write the following NgRx reducer:

import { createReducer, immerOn } from 'ngrx-immer';
import { addUser, updateUser, deleteUser } from './user.actions';

export interface User {
  name: string;
  age: number;
}

export const initialState: User[] = [];

export const _userReducer = createReducer(
  initialState,
  immerOn(addUser, (state, { user }) => {
    state.push(user);
  }),
  immerOn(updateUser, (state, { user, name }) => {
    const index = state.findIndex(u => u.name === user.name);
    state[index].name = name;
  }),
  immerOn(deleteUser, (state, { user }) => {
    const index = state.findIndex(u => u.name === user.name);
    state.splice(index, 1);
  })
);

As you can see, the immerOnmethod provides a more intuitive and direct way of updating the state. It allows you to modify the state directly instead of creating a new object with the spread operator.

You don’t have to refactor your app — mix and match !

No need to rewrite all of your reducers.

You can use immerOn in the places that are the most complicated.

You can combine the immerOn method from the ngrx-immer library with the on method from the @ngrx/store library.

import { createReducer, on, immerOn } from 'ngrx-immer';
import { addUser, updateUser, deleteUser, togglePremium } from './user.actions';

export interface User {
  name: string;
  age: number;
  premium: boolean;
}

export const initialState: User[] = [];

export const _userReducer = createReducer(
  initialState,
  on(addUser, (state, { user }) => [...state, user]),
  immerOn(updateUser, (state, { user, name }) => {
    const index = state.findIndex(u => u.name === user.name);
    state[index].name = name;
  }),
  immerOn(deleteUser, (state, { user }) => {
    const index = state.findIndex(u => u.name === user.name);
    state.splice(index, 1);
  }),
  immerOn(togglePremium, (state, { user }) => {
    const index = state.findIndex(u => u.name === user.name);
    state[index].premium = !state[index].premium;
  })
);

Almost no effort to use but makes a giant difference to your code readability and complexity.

rxjs

Worthy of becoming part of the NgRx core library?

Do you think this should become a part of core NgRx library ? I vote that it should !

Why?

Installing multiple dependencies maintained outside of core libraries is always a risk of something that can stop being maintained.

Smaller lib, less maintainers = bigger risk.

And something as useful as this could come in bundle with NgRx.

Don’t agree ? Let’s discuss.

Sources

All credits go to fantastic Tim Deschryver — the author of this library.

https://github.com/timdeschryver/ngrx-immer

...

🔧 Make your Angular NgRx reducers an eye candy 🍬 using this fantastic library


📈 136.86 Punkte
🔧 Programmierung

🔧 Announcing NgRx v17: Introducing NgRx Signals, Operators, Performance Improvements, Workshops, and more!


📈 46.75 Punkte
🔧 Programmierung

📰 ANGULAR (FULL APP) WITH ANGULAR MATERIAL, ANGULARFIRE & NGRX


📈 45.8 Punkte
📰 Alle Kategorien

🔧 Angular Router URL Parameters Using NgRx Router Store


📈 39.33 Punkte
🔧 Programmierung

🔧 How to Debug NgRx Using REDUX DevTools in Angular


📈 39.33 Punkte
🔧 Programmierung

🔧 How to Handle Side Effects in Angular Using NgRx Effects


📈 39.33 Punkte
🔧 Programmierung

🔧 State Management in Angular using NgRx Signal Store


📈 39.33 Punkte
🔧 Programmierung

🔧 How to upgrade your Angular web project from Angular V13 to Angular V17


📈 36.96 Punkte
🔧 Programmierung

🔧 When and Why to Use REDUX NgRx in Angular


📈 34.59 Punkte
🔧 Programmierung

🔧 Harnessing NgRx for Effortless Angular State Management


📈 34.59 Punkte
🔧 Programmierung

🔧 Angular Addicts #27: NgRx 18, New RFC: DomRef API, Web storage with Signals & more


📈 34.59 Punkte
🔧 Programmierung

🔧 Ng-News 24/24: Vertical Architectures, WebAssembly, Angular v9's Secret, NgRx


📈 34.59 Punkte
🔧 Programmierung

🔧 Episode 24/24: Vertical Architectures, WebAssembly, Angular v9's Secret, NgRx


📈 34.59 Punkte
🔧 Programmierung

🔧 How to Use NgRx Selectors in Angular


📈 34.59 Punkte
🔧 Programmierung

🔧 State Management in Angular: NgRx vs NGXS


📈 34.59 Punkte
🔧 Programmierung

🔧 how to import ngrx StoreModule in angular 17 config file


📈 34.59 Punkte
🔧 Programmierung

🔧 State Management in Angular with NgRx


📈 34.59 Punkte
🔧 Programmierung

🔧 What's new in NgRx Version 12 of Angular?


📈 34.59 Punkte
🔧 Programmierung

🪟 Psycho Krieg and the Fantastic Fustercluck DLC is not 'fantastic'


📈 34.06 Punkte
🪟 Windows Tipps

🔧 Comparing React state tools: Mutative vs. Immer vs. reducers


📈 33.7 Punkte
🔧 Programmierung

🐧 A little eye-candy widget to lure people into using tmux ;)


📈 33.65 Punkte
🐧 Linux Tipps

🔧 Angular Addicts #25: Angular and Wiz will be merged, the differences between React and Angular & more


📈 33.63 Punkte
🔧 Programmierung

🕵️ CVE-2023-26116 | angular angular.copy redos (SNYK-JS-ANGULAR-3373044)


📈 33.63 Punkte
🕵️ Sicherheitslücken

📰 Ähnliche Spiele wie Candy Crush Saga: 9 Alternativen zu Candy Crush


📈 32.05 Punkte
🖥️ Betriebssysteme

🔧 ** Candy Candy y la Lógica: Una Dulce Analogía en el Mundo Digital **👱‍♀️


📈 32.05 Punkte
🔧 Programmierung

🔧 rx-angular/state - a library for managing states of an Angular application


📈 30.26 Punkte
🔧 Programmierung

📰 5 most beautiful Linux distributions: 'Equal parts user-friendly and eye candy'


📈 28.91 Punkte
📰 IT Nachrichten

📰 4 ways to add more eye candy to the GNOME desktop


📈 28.91 Punkte
📰 IT Nachrichten

📰 How to add more eye candy to the GNOME desktop


📈 28.91 Punkte
📰 IT Nachrichten

🐧 Enlightenment 0.26 and EFL 1.27: From Eye-Candy to High Performance


📈 28.91 Punkte
🐧 Linux Tipps

📰 This Is the Eye-Candy Windows Longhorn Login Screen That Never Launched


📈 28.91 Punkte
📰 IT Security Nachrichten

📰 This Is the Eye-Candy Windows 10 Lock Screen That’ll Never Happen


📈 28.91 Punkte
📰 IT Security Nachrichten

📰 Customized elementary OS Is a Smart Mix of Eye-Candy UI and Improved Usability


📈 28.91 Punkte
📰 IT Security Nachrichten

matomo