Skip to content

Commit 867b24c

Browse files
docs: add feature examples + devtool pic
1 parent f051f12 commit 867b24c

1 file changed

Lines changed: 67 additions & 11 deletions

File tree

v21-blog-post.md

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
# NgRx Toolkit v21
22

3-
## TODO: Explain Toolkit
4-
5-
- It existed alreday at a time where the signalStore was not even stable
6-
- It sees itself as rich set of extensions you need in typical Angular applications.
7-
- Mention redux extension, but also maybe 2 further features from the beginning
8-
9-
The Toolkit originates as far back as when the SignalStore was not even marked stable.
3+
The NgRx Toolkit originates as far back as when the SignalStore was not even marked stable.
104
As the package was in its early stages, community requests for various functionality
115
poured in. However, the NgRx team wanted to focus on core functionality that would be
126
broadly usable and supported to their full standard. But they provided the tools for the
137
community to make its own tools, the `signalStoreFeature` function. Rainer Hahnekamp
148
saw the opportunity to house these tools inside what is now the NgRx Toolkit.
159

10+
```ts
11+
import { withStorageSync } from '@angular-architects/ngrx-toolkit';
12+
import { withImmutableState } from '@angular-architects/ngrx-toolkit';
13+
import { withCallState } from '@angular-architects/ngrx-toolkit';
14+
import { withDevtools } from '@angular-architects/ngrx-toolkit';
15+
16+
const UserStore = signalStore(
17+
// will throw runtime error if unitended mutation happens
18+
withImmutableState({ name: 'John' }),
19+
20+
// automatically synchronizes state to localStorage on each change via the key 'user'
21+
// (also can do sesssion storage and IndexedDB
22+
withStorageSync('user'),
23+
24+
// exposes `setLoaded()/setLoading()/setError(error)`
25+
withCallState(),
26+
27+
// allows Redux Devtools (even without Redux!)
28+
withDevtools('user'),
29+
30+
// and more! https://ngrx-toolkit.angulararchitects.io/docs/extensions
31+
);
32+
```
33+
1634
The NgRx Toolkit sees itself as rich set of extensions that you need in typical Angular applications.
1735
Core functionality and its history:
1836

@@ -26,7 +44,7 @@ As easy as `withStorageSync('storeNameHere')`. IndexedDB was added last year by
2644
- `withFeature` of the SignalStore started out in the Toolkit as `withFeatureFactory`
2745
- Other features can be found in the documentation: https://ngrx-toolkit.angulararchitects.io/docs/extensions
2846

29-
// TODO - some snippets? Screenshot(s)?
47+
<img width="2204" height="1464" alt="Redux Devtools showing application state from withStorageSync()" src="https://github.com/user-attachments/assets/4dcc0b0f-27e4-4e95-ac0e-24dcd06716c0" />
3048

3149
## But first: v20 minor features: `withResource`/`withEntityResources`/Mutations
3250

@@ -41,9 +59,47 @@ The Mutations API came in a later minor, providing the other pieces of the REST
4159
standalone functions (`httpMutation`/`rxMutation`), as well as in a `withMutation` feature. The API was inspired by Angular Query and Marko Stanimirović's
4260
proposed mutations API for Angular. We also had internal discussions with Alex Rickabaugh on our design.
4361

44-
// TODO - snippets?
62+
```ts
63+
import { httpMutation, rxMutation, withMutations } from '@angular-architects/ngrx-toolkit';
64+
import { withResource, withEntityResources } from '@angular-architects/ngrx-toolkit';
65+
66+
export const UserStore = signalStore(
67+
withState({ userId: undefined as number | undefined }),
68+
withResource(({ userId }) => ({
69+
detail: httpResource<User>(() => (userId === undefined ? undefined : `/user/${userId}`)),
70+
})),
71+
withMutations((store, userService = inject(UserService)) => ({
72+
saveUserDetail: rxMutation({
73+
operation: (params: Params) => {
74+
return userService.saveUserDetail(store.counter(), params.value);
75+
},
76+
onSuccess: (result) => {
77+
// ...
78+
},
79+
onError: (error) => {
80+
// ...
81+
},
82+
}),
83+
saveToServer: httpMutation({
84+
request: (_: void) => ({
85+
url: `https://httpbin.org/post`,
86+
method: 'POST',
87+
body: { counter: store.counter() },
88+
}),
89+
parse: (response) => response as UserResponse,
90+
onSuccess: (result) => {
91+
// ...
92+
},
93+
onError: (error) => {
94+
// ...
95+
},
96+
}),
97+
})),
98+
withEntityResources(() => resource({ loader: () => Promise.resolve([] as User[]), defaultValue: [] })),
99+
);
100+
```
45101

46-
## TODO: v21
102+
## v21
47103

48104
Finally, actual Toolkit v21 notes. Future posts that are not our debut blogpost for the library won't always have a three act structure with a detailed backstory, we promise.
49105

@@ -101,7 +157,7 @@ In NgRx Toolkit v21, we are fixing this with `withTrackedReducer()`, an alternat
101157
// TODO - snippet with imports
102158
```
103159

104-
## TODO (also decide location) - where to mention Murat's OpenAPI library based on the toolkit
160+
## ngrx-toolkit-openapi-gen
105161

106162
We got a fantastic Christmas present from Murat Sari (TODO: social to link to?)
107163

0 commit comments

Comments
 (0)