You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/createSelector.mdx
+41
Original file line number
Diff line number
Diff line change
@@ -19,3 +19,44 @@ For more details on using `createSelector`, see:
19
19
> **Note**: Prior to v0.7, RTK re-exported `createSelector` from [`selectorator`](https://github.com/planttheidea/selectorator), which
20
20
> allowed using string keypaths as input selectors. This was removed, as it ultimately did not provide enough benefits, and
21
21
> the string keypaths made static typing for selectors difficult.
22
+
23
+
# `createDraftSafeSelector`
24
+
25
+
In general, we recommend against using selectors inside of reducers:
26
+
27
+
- Selectors typically expect the entire Redux state object as an argument, while slice reducers only have access to a specific subset of the entire Redux state
28
+
- Reselect's `createSelector` relies on reference comparisons to determine if inputs have changed, and if an Immer Proxy-wrapped draft value is passed in to a selector, the selector may see the same reference and think nothing has changed.
29
+
30
+
However, some users have requested the ability to create selectors that will work correctly inside of Immer-powered reducers. One use case for this might be collecting an ordered set of items when using `createEntityAdapter, such as`const orderedTodos = todosSelectors.selectAll(todosState)`, and then using`orderedTodos` in the rest of the reducer logic.
31
+
32
+
Besides re-exporting `createSelector`, RTK also exports a wrapped version of `createSelector` named `createDraftSafeSelector` that allows you to create selectors that can safely be used inside of `createReducer` and `createSlice` reducers with Immer-powered mutable logic. When used with plain state values, the selector will still memoize normally based on the inputs. But, when used with Immer draft values, the selector will err on the side of recalculating the results, just to be safe.
33
+
34
+
All selectors created by `entityAdapter.getSelectors` are "draft safe" selectors by default.
0 commit comments