Skip to content

Commit 8f229d1

Browse files
author
ben.durrant
committed
add to docs
1 parent 7996827 commit 8f229d1

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

docs/api/createEntityAdapter.mdx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ All three options will insert _new_ entities into the list. However they differ
228228

229229
:::
230230

231-
232231
Each method has a signature that looks like:
233232

234233
```ts no-transpile
@@ -284,9 +283,36 @@ The entity adapter will contain a `getSelectors()` function that returns a set o
284283

285284
Each selector function will be created using the `createSelector` function from Reselect, to enable memoizing calculation of the results.
286285

286+
:::tip
287+
288+
The `createSelector` instance used can be replaced, by passing it as a second parameter:
289+
290+
```js
291+
import {
292+
createDraftSafeSelectorCreator,
293+
weakMapMemoize,
294+
} from '@reduxjs/toolkit'
295+
296+
const createWeakMapDraftSafeSelector =
297+
createDraftSafeSelectorCreator(weakMapMemoize)
298+
299+
const simpleSelectors = booksAdapter.getSelectors(
300+
undefined,
301+
createWeakMapDraftSafeSelector
302+
)
303+
const globalizedSelectors = booksAdapter.getSelectors(
304+
(state) => state.books,
305+
createWeakMapDraftSafeSelector
306+
)
307+
```
308+
309+
If no instance is passed, it will default to [`createDraftSafeSelector`](./createSelector#createDraftSafeSelector).
310+
311+
:::
312+
287313
Because selector functions are dependent on knowing where in the state tree this specific entity state object is kept, `getSelectors()` can be called in two ways:
288314

289-
- If called without any arguments, it returns an "unglobalized" set of selector functions that assume their `state` argument is the actual entity state object to read from.
315+
- If called without any arguments (or with undefined as the first parameter), it returns an "unglobalized" set of selector functions that assume their `state` argument is the actual entity state object to read from.
290316
- It may also be called with a selector function that accepts the entire Redux state tree and returns the correct entity state object.
291317

292318
For example, the entity state for a `Book` type might be kept in the Redux state tree as `state.books`. You can use `getSelectors()` to read from that state in two ways:
@@ -358,12 +384,8 @@ const booksSlice = createSlice({
358384
},
359385
})
360386

361-
const {
362-
bookAdded,
363-
booksLoading,
364-
booksReceived,
365-
bookUpdated,
366-
} = booksSlice.actions
387+
const { bookAdded, booksLoading, booksReceived, bookUpdated } =
388+
booksSlice.actions
367389

368390
const store = configureStore({
369391
reducer: {

docs/api/createSelector.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,25 @@ After executing that, `unsafe1` and `unsafe2` will be of the same value, because
6262
executed on the same object - but `safe2` will actually be different from `safe1` (with the updated value of `2`),
6363
because the safe selector detected that it was executed on a Immer draft object and recalculated using the current
6464
value instead of returning a cached value.
65+
66+
:::tip `createDraftSafeSelectorCreator`
67+
68+
RTK also exports a `createDraftSafeSelectorCreator` function, the "draft safe" equivalent of [`createSelectorCreator`](https://github.com/reduxjs/reselect#createselectorcreatormemoize-memoizeoptions).
69+
70+
```js
71+
import {
72+
createDraftSafeSelectorCreator,
73+
weakMapMemoize,
74+
} from '@reduxjs/toolkit'
75+
76+
const createWeakMapDraftSafeSelector =
77+
createDraftSafeSelectorCreator(weakMapMemoize)
78+
79+
const selectSelf = (state: State) => state
80+
const draftSafeSelector = createWeakMapDraftSafeSelector(
81+
selectSelf,
82+
(state) => state.value
83+
)
84+
```
85+
86+
:::

0 commit comments

Comments
 (0)