• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Immer wrappers around NgRx methods createReducer, on, and ComponentStore

ngrx-immer

Immer wrappers around NgRx methods to mutate state easily

Installation

npm install ngrx-immer

Do not forget to install immer

Resources

createImmerReducer

Creates an NgRx reducer, but allows you to mutate state without having to use to spread operator.

  • Use it when you want to go Immer all the way
  • Caveat, you have to return the state with each on method
import { createImmerReducer } from 'ngrx-immer/store';

const todoReducer = createImmerReducer(
	{ todos: [] },
	on(newTodo, (state, action) => {
		state.todos.push({ text: action.todo, completed: false });
		return state;
	}),
	on(completeTodo, (state, action) => {
		state.todos[action.index].completed = true;
		return state;
	}),
);

immerOn

Creates an NgRx reducer, but allows you to mutate state without having to use to spread operator.

  • Use it when you want to go sprinkle a little bit of Immer for more complex cases
import { immerOn } from 'ngrx-immer/store';

const todoReducer = createReducer(
	{ todos: [] },
	on(newTodo, (state, action) => {
		return {
			...state,
			todos: [...state.todos, action.todo],
		};
	}),
	immerOn(completeTodo, (state, action) => {
		state.todos[action.index].completed = true;
	}),
);

ImmerComponentStore

Wraps Immer around the Component Store updater and setState methods.

import { ImmerComponentStore } from 'ngrx-immer/component-store';

@Injectable()
export class MoviesStore extends ImmerComponentStore<MoviesState> {
	constructor() {
		super({ movies: [] });
	}

	readonly addMovie = this.updater((state, movie: Movie) => {
		state.movies.push(movie);
	});
}

immerReducer

Inspired by Alex Okrushko, immerReducer is a reducer method that uses the Immer produce method. This method is used by all of the methods in ngrx-immer provides.

Migrating from ngrx-etc to ngrx-immer

You can execute the following script in your project, this script replaces everything from ngrx-etc to the ngrx-immer equivalent.

npx https://gist.github.com/timdeschryver/efdded8b72bd36ac0a2bc719eca1f161

Don't forget to install ngrx-immer and immer after running the above script.

Old (ngrx-etc) New (ngrx-immer)
mutableOn immerOn
createMutableReducer createImmerReducer
ImmerComponentStore
immerReducer

FAQ

More Repositories

1

rx-query

TypeScript
200
star
2

eslint-plugin-ngrx

TypeScript
140
star
3

zod-fixture

Creating fixtures based on zod schemas
TypeScript
81
star
4

ngrx-etc

Utility functions for NgRx
TypeScript
70
star
5

hacktoberfest-2019-angular

Share your favorite Angular resources of 2019
TypeScript
64
star
6

HowToTestYourCsharpWebApi

C#
48
star
7

xstate-table

JavaScript
44
star
8

ngrx-tslint-rules

TSLint rules for NgRx
TypeScript
37
star
9

timdeschryver.dev

CSS
33
star
10

ngrx-family-grocery-list

TypeScript
29
star
11

ng-signal-forms

TypeScript
28
star
12

query-state

TypeScript
23
star
13

frontal

An Angular select/dropdown component
TypeScript
20
star
14

new-blog-post

TypeScript
19
star
15

ngrx-immer-example

TypeScript
17
star
16

docs-md

Generate markdown docs for your project's public API
TypeScript
16
star
17

svelte-utils

TypeScript
16
star
18

rxjs-operator-counter

Count the number of RxJS operators used in an application
TypeScript
16
star
19

ngrx-tslint

TypeScript
9
star
20

ng-fourteen

TypeScript
7
star
21

vscode-chameleon

Randomize your Visual Studio Code theme, font and icons on startup
TypeScript
6
star
22

eslint-plugin-rxjs-fixers

TypeScript
6
star
23

angular-forms-guide

TypeScript
5
star
24

csharp-benchmarks

C#
4
star
25

ng-standalone-migration

TypeScript
4
star
26

ng-xstate

TypeScript
3
star
27

vscode-dev-to

Dev.to in Visual Studio Code
TypeScript
3
star
28

atl-good-testing-practices

TypeScript
2
star
29

ngrx-persist-state

TypeScript
2
star
30

tl-user-event-mod

Moves @testing-library/user-event to build-in @testing-library/dom
TypeScript
1
star
31

router-guards

TypeScript
1
star
32

rxjs-fixers-morph

TypeScript
1
star
33

playwright-sharding

TypeScript
1
star
34

ngrx-fizzbuzz

TypeScript
1
star
35

AspireWithAngular

C#
1
star
36

ODataSandboxApi

C#
1
star
37

svelte-xstate-todomvc

Created with CodeSandbox
JavaScript
1
star
38

ng-msw

TypeScript
1
star