Skip to content

Commit 313e3ce

Browse files
authored
Remove slice selectors (#193)
* Remove slice selectors * Update createSlice docs to remove selector references
1 parent 659ff14 commit 313e3ce

7 files changed

+10
-111
lines changed

docs/api/createSlice.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ hide_title: true
77

88
# `createSlice`
99

10-
A function that accepts an initial state, an object full of reducer functions, and optionally a "slice name", and automatically generates action creators, action types, and selectors that correspond to the reducers and state.
10+
A function that accepts an initial state, an object full of reducer functions, and optionally a "slice name",
11+
and automatically generates action creators and action types that correspond to the reducers and state.
1112

1213
## Parameters
1314

@@ -19,7 +20,7 @@ function createSlice({
1920
reducers: Object<string, ReducerFunction>
2021
// The initial state for the reducer
2122
initialState: any,
22-
// An optional name, used in action types and selectors
23+
// An optional name, used in action types
2324
slice?: string,
2425
// An additional object of "case reducers". Keys should be other action types.
2526
extraReducers?: Object<string, ReducerFunction>
@@ -45,16 +46,7 @@ The initial state value for this slice of state.
4546

4647
### `slice`
4748

48-
An optional string name for this slice of state.
49-
50-
The slice name is used in two ways.
51-
52-
First, if provided, generated action type constants will use this as a prefix.
53-
54-
Second, it affects the name and behavior of the generated selector. If provided, a selector named after the slice
55-
will be generated. This selector assume the slice data exists in an object, with the slice name as the key, and will
56-
return the value at that key name. If not provided, a selector named `getState` will be generated that just returns
57-
its argument.
49+
An optional string name for this slice of state. Generated action type constants will use this as a prefix.
5850

5951
### `extraReducers`
6052

@@ -84,7 +76,6 @@ to force the TS compiler to accept the computed property.)
8476
slice : string,
8577
reducer : ReducerFunction,
8678
actions : Object<string, ActionCreator},
87-
selectors : Object<string, Selector>
8879
}
8980
```
9081

@@ -93,8 +84,6 @@ and included in the result's `actions` field using the same function name.
9384

9485
The generated `reducer` function is suitable for passing to the Redux `combineReducers` function as a "slice reducer".
9586

96-
The generated selector function will be available in the result's `selectors` field.
97-
9887
You may want to consider destructuring the action creators and exporting them individually, for ease of searching
9988
for references in a larger codebase.
10089

@@ -156,9 +145,4 @@ console.log(`${counter.actions.decrement}`)
156145
// -> "counter/decrement"
157146
store.dispatch(user.actions.setUserName('eric'))
158147
// -> { counter: 6, user: { name: 'eric', age: 22} }
159-
const state = store.getState()
160-
console.log(user.selectors.getUser(state))
161-
// -> { name: 'eric', age: 22 }
162-
console.log(counter.selectors.getCounter(state))
163-
// -> 6
164148
```

src/configureStore.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { configureStore } from './configureStore'
22
import * as redux from 'redux'
33
import * as devtools from 'redux-devtools-extension'
4-
import {
5-
StoreCreator,
6-
StoreEnhancer,
7-
StoreEnhancerStoreCreator,
8-
Reducer,
9-
AnyAction
10-
} from 'redux'
4+
import { StoreEnhancer, StoreEnhancerStoreCreator } from 'redux'
115

126
describe('configureStore', () => {
137
jest.spyOn(redux, 'applyMiddleware')

src/createSlice.test.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createAction, PayloadAction } from './createAction'
33

44
describe('createSlice', () => {
55
describe('when slice is empty', () => {
6-
const { actions, reducer, selectors } = createSlice({
6+
const { actions, reducer } = createSlice({
77
reducers: {
88
increment: state => state + 1,
99
multiply: (state, action: PayloadAction<number>) =>
@@ -43,20 +43,10 @@ describe('createSlice', () => {
4343
expect(reducer(2, actions.multiply(3))).toEqual(6)
4444
})
4545
})
46-
47-
describe('when using selectors', () => {
48-
it('should create selector with correct name', () => {
49-
expect(selectors.hasOwnProperty('getState')).toBe(true)
50-
})
51-
52-
it('should return the slice state data', () => {
53-
expect(selectors.getState(2)).toEqual(2)
54-
})
55-
})
5646
})
5747

5848
describe('when passing slice', () => {
59-
const { actions, reducer, selectors } = createSlice({
49+
const { actions, reducer } = createSlice({
6050
reducers: {
6151
increment: state => state + 1
6252
},
@@ -78,14 +68,6 @@ describe('createSlice', () => {
7868
it('should return the correct value from reducer', () => {
7969
expect(reducer(undefined, actions.increment())).toEqual(1)
8070
})
81-
82-
it('should create selector with correct name', () => {
83-
expect(selectors.hasOwnProperty('getCool')).toBe(true)
84-
})
85-
86-
it('should return the slice state data', () => {
87-
expect(selectors.getCool({ cool: 2 })).toEqual(2)
88-
})
8971
})
9072

9173
describe('when mutating state object', () => {

src/createSlice.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ActionCreatorWithPreparedPayload
99
} from './createAction'
1010
import { createReducer, CaseReducers, CaseReducer } from './createReducer'
11-
import { createSliceSelector, createSelectorName } from './sliceSelector'
1211

1312
/**
1413
* An action creator atttached to a slice.
@@ -36,14 +35,6 @@ export interface Slice<
3635
* reducer.
3736
*/
3837
actions: ActionCreators
39-
40-
/**
41-
* Selectors for the slice reducer state. `createSlice()` inserts a single
42-
* selector that returns the entire slice state and whose name is
43-
* automatically derived from the slice name (e.g., `getCounter` for a slice
44-
* named `counter`).
45-
*/
46-
selectors: { [key: string]: (state: any) => State }
4738
}
4839

4940
/**
@@ -54,8 +45,7 @@ export interface CreateSliceOptions<
5445
CR extends SliceCaseReducers<State, any> = SliceCaseReducers<State, any>
5546
> {
5647
/**
57-
* The slice's name. Used to namespace the generated action types and to
58-
* name the selector for retrieving the reducer's state.
48+
* The slice's name. Used to namespace the generated action types.
5949
*/
6050
slice?: string
6151

@@ -156,7 +146,7 @@ function getType(slice: string, actionKey: string): string {
156146
/**
157147
* A function that accepts an initial state, an object full of reducer
158148
* functions, and optionally a "slice name", and automatically generates
159-
* action creators, action types, and selectors that correspond to the
149+
* action creators and action types that correspond to the
160150
* reducers and state.
161151
*
162152
* The `reducer` argument is passed to `createReducer()`.
@@ -205,14 +195,9 @@ export function createSlice<
205195
{} as any
206196
)
207197

208-
const selectors = {
209-
[createSelectorName(slice)]: createSliceSelector(slice)
210-
}
211-
212198
return {
213199
slice,
214200
reducer,
215-
actions: actionMap,
216-
selectors
201+
actions: actionMap
217202
}
218203
}

src/sliceSelector.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/sliceSelector.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

type-tests/files/createSlice.typetest.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ function expectType<T>(t: T) {
4040

4141
// typings:expect-error
4242
slice.actions.other(1)
43-
44-
/* Selector */
45-
46-
const value: number = slice.selectors.getCounter(0)
47-
48-
// typings:expect-error
49-
const stringValue: string = slice.selectors.getCounter(0)
5043
}
5144

5245
/*

0 commit comments

Comments
 (0)