@@ -324,6 +324,105 @@ reducers: (create) => {
324
324
325
325
:::
326
326
327
+ ##### ` create.entityMethods ` (` entityMethodsCreator ` )
328
+
329
+ Creates a set of reducers for managing a normalized entity state, based on a provided [ adapter] ( ./createEntityAdapter ) .
330
+
331
+ ``` ts
332
+ import {
333
+ createEntityAdapter ,
334
+ buildCreateSlice ,
335
+ entityMethodsCreator ,
336
+ } from ' @reduxjs/toolkit'
337
+
338
+ const createAppSlice = buildCreateSlice ({
339
+ creators: { entityMethods: entityMethodsCreator },
340
+ })
341
+
342
+ interface Post {
343
+ id: string
344
+ text: string
345
+ }
346
+
347
+ const postsAdapter = createEntityAdapter <Post >()
348
+
349
+ const postsSlice = createAppSlice ({
350
+ name: ' posts' ,
351
+ initialState: postsAdapter .getInitialState (),
352
+ reducers : (create ) => ({
353
+ ... create .entityMethods (postsAdapter ),
354
+ }),
355
+ })
356
+
357
+ export const { setOne, upsertMany, removeAll, ... etc } = postsSlice .actions
358
+ ```
359
+
360
+ :::caution
361
+
362
+ Because this creator returns an object of multiple reducer definitions, it should be spread into the final object returned by the ` reducers ` callback.
363
+
364
+ :::
365
+
366
+ ** Parameters**
367
+
368
+ - ** adapter** The [ adapter] ( ../api/createEntityAdapter ) to use.
369
+ - ** config** The configuration object. (optional)
370
+
371
+ The configuration object can contain some of the following options:
372
+
373
+ ** ` selectEntityState ` **
374
+
375
+ A selector to retrieve the entity state from the slice state. Defaults to ` state => state ` , but should be provided if the entity state is nested.
376
+
377
+ ``` ts no-transpile
378
+ const postsSlice = createAppSlice ({
379
+ name: ' posts' ,
380
+ initialState: { posts: postsAdapter .getInitialState () },
381
+ reducers : (create ) => ({
382
+ ... create .entityMethods (postsAdapter , {
383
+ selectEntityState : (state ) => state .posts ,
384
+ }),
385
+ }),
386
+ })
387
+ ```
388
+
389
+ ** ` name ` , ` pluralName ` **
390
+
391
+ It's often desirable to modify the reducer names to be specific to the data type being used. These options allow you to do that simply.
392
+
393
+ ``` ts no-transpile
394
+ const postsSlice = createAppSlice ({
395
+ name: ' posts' ,
396
+ initialState: postsAdapter .getInitialState (),
397
+ reducers : (create ) => ({
398
+ ... create .entityMethods (postsAdapter , {
399
+ name: ' post' ,
400
+ }),
401
+ }),
402
+ })
403
+
404
+ const { addOnePost, upsertManyPosts, removeAllPosts, ... etc } =
405
+ postsSlice .actions
406
+ ```
407
+
408
+ ` pluralName ` defaults to ` name + 's' ` , but can be provided if this isn't desired.
409
+
410
+ ``` ts no-transpile
411
+ const gooseSlice = createAppSlice ({
412
+ name: ' geese' ,
413
+ initialState: gooseAdapter .getInitialState (),
414
+ reducers : (create ) => ({
415
+ ... create .entityMethods (gooseAdapter , {
416
+ name: ' goose' ,
417
+ pluralName: ' geese' ,
418
+ }),
419
+ }),
420
+ })
421
+
422
+ const { addOneGoose, upsertManyGeese, removeAllGeese, ... etc } =
423
+ gooseSlice .actions
424
+ ```
425
+
327
426
## Writing your own creators
328
427
329
428
In version v2.3.0, we introduced a system for including your own creators.
@@ -338,7 +437,7 @@ For example, the `create.preparedReducer` creator uses a definition that looks l
338
437
339
438
The callback form of ` reducers ` should return an object of reducer definitions, by calling creators and nesting the result of each under a key.
340
439
341
- ``` js no-transpile
440
+ ``` js
342
441
reducers : (create ) => ({
343
442
addTodo: create .preparedReducer (
344
443
(todo ) => ({ payload: { id: nanoid (), ... todo } }),
0 commit comments