Skip to content

Commit 1789d80

Browse files
committed
add note re: unwrapped
1 parent b2e1579 commit 1789d80

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/api/createSlice.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,40 @@ const { selectValue } = counterSlice.selectors
513513
console.log(selectValue({ counter: { value: 2 } })) // 2
514514
```
515515
516+
:::note
517+
518+
The original selector passed is attached to the wrapped selector as `.unwrapped`. For example:
519+
520+
```ts
521+
import { createSlice, createSelector } from '@reduxjs/toolkit'
522+
523+
interface CounterState {
524+
value: number
525+
}
526+
527+
const counterSlice = createSlice({
528+
name: 'counter',
529+
initialState: { value: 0 } as CounterState,
530+
reducers: {
531+
// omitted
532+
},
533+
selectors: {
534+
selectDouble: createSelector(
535+
(sliceState: CounterState) => sliceState.value,
536+
(value) => value * 2
537+
),
538+
},
539+
})
540+
541+
const { selectDouble } = counterSlice.selectors
542+
543+
console.log(selectDouble({ counter: { value: 2 } })) // 4
544+
console.log(selectDouble({ counter: { value: 3 } })) // 6
545+
console.log(selectDouble.unwrapped.recomputations) // 2
546+
```
547+
548+
:::
549+
516550
#### `getSelectors`
517551
518552
`slice.getSelectors` is called with a single parameter, a `selectState` callback. This function should receive the store root state (or whatever you expect to call the resulting selectors with) and return the slice state.

0 commit comments

Comments
 (0)