From 1789d80bcdf1eefa4ac5e959c11f5df2283d3bda Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Tue, 14 Nov 2023 23:09:42 +0000 Subject: [PATCH] add note re: unwrapped --- docs/api/createSlice.mdx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/api/createSlice.mdx b/docs/api/createSlice.mdx index 690840868e..dbeab9b979 100644 --- a/docs/api/createSlice.mdx +++ b/docs/api/createSlice.mdx @@ -513,6 +513,40 @@ const { selectValue } = counterSlice.selectors console.log(selectValue({ counter: { value: 2 } })) // 2 ``` +:::note + +The original selector passed is attached to the wrapped selector as `.unwrapped`. For example: + +```ts +import { createSlice, createSelector } from '@reduxjs/toolkit' + +interface CounterState { + value: number +} + +const counterSlice = createSlice({ + name: 'counter', + initialState: { value: 0 } as CounterState, + reducers: { + // omitted + }, + selectors: { + selectDouble: createSelector( + (sliceState: CounterState) => sliceState.value, + (value) => value * 2 + ), + }, +}) + +const { selectDouble } = counterSlice.selectors + +console.log(selectDouble({ counter: { value: 2 } })) // 4 +console.log(selectDouble({ counter: { value: 3 } })) // 6 +console.log(selectDouble.unwrapped.recomputations) // 2 +``` + +::: + #### `getSelectors` `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.