diff --git a/docs/api/createSlice.md b/docs/api/createSlice.md index debe9696f8..19f0b0657f 100644 --- a/docs/api/createSlice.md +++ b/docs/api/createSlice.md @@ -7,7 +7,8 @@ hide_title: true # `createSlice` -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. +A function that accepts an initial state, an object full of reducer functions, and optionally a "slice name", +and automatically generates action creators and action types that correspond to the reducers and state. ## Parameters @@ -19,7 +20,7 @@ function createSlice({ reducers: Object // The initial state for the reducer initialState: any, - // An optional name, used in action types and selectors + // An optional name, used in action types slice?: string, // An additional object of "case reducers". Keys should be other action types. extraReducers?: Object @@ -45,16 +46,7 @@ The initial state value for this slice of state. ### `slice` -An optional string name for this slice of state. - -The slice name is used in two ways. - -First, if provided, generated action type constants will use this as a prefix. - -Second, it affects the name and behavior of the generated selector. If provided, a selector named after the slice -will be generated. This selector assume the slice data exists in an object, with the slice name as the key, and will -return the value at that key name. If not provided, a selector named `getState` will be generated that just returns -its argument. +An optional string name for this slice of state. Generated action type constants will use this as a prefix. ### `extraReducers` @@ -84,7 +76,6 @@ to force the TS compiler to accept the computed property.) slice : string, reducer : ReducerFunction, actions : Object } ``` @@ -93,8 +84,6 @@ and included in the result's `actions` field using the same function name. The generated `reducer` function is suitable for passing to the Redux `combineReducers` function as a "slice reducer". -The generated selector function will be available in the result's `selectors` field. - You may want to consider destructuring the action creators and exporting them individually, for ease of searching for references in a larger codebase. @@ -156,9 +145,4 @@ console.log(`${counter.actions.decrement}`) // -> "counter/decrement" store.dispatch(user.actions.setUserName('eric')) // -> { counter: 6, user: { name: 'eric', age: 22} } -const state = store.getState() -console.log(user.selectors.getUser(state)) -// -> { name: 'eric', age: 22 } -console.log(counter.selectors.getCounter(state)) -// -> 6 ``` diff --git a/src/configureStore.test.ts b/src/configureStore.test.ts index c5fb918d33..fd588abb4d 100644 --- a/src/configureStore.test.ts +++ b/src/configureStore.test.ts @@ -1,13 +1,7 @@ import { configureStore } from './configureStore' import * as redux from 'redux' import * as devtools from 'redux-devtools-extension' -import { - StoreCreator, - StoreEnhancer, - StoreEnhancerStoreCreator, - Reducer, - AnyAction -} from 'redux' +import { StoreEnhancer, StoreEnhancerStoreCreator } from 'redux' describe('configureStore', () => { jest.spyOn(redux, 'applyMiddleware') diff --git a/src/createSlice.test.ts b/src/createSlice.test.ts index 1186a6aec9..05166924ff 100644 --- a/src/createSlice.test.ts +++ b/src/createSlice.test.ts @@ -3,7 +3,7 @@ import { createAction, PayloadAction } from './createAction' describe('createSlice', () => { describe('when slice is empty', () => { - const { actions, reducer, selectors } = createSlice({ + const { actions, reducer } = createSlice({ reducers: { increment: state => state + 1, multiply: (state, action: PayloadAction) => @@ -43,20 +43,10 @@ describe('createSlice', () => { expect(reducer(2, actions.multiply(3))).toEqual(6) }) }) - - describe('when using selectors', () => { - it('should create selector with correct name', () => { - expect(selectors.hasOwnProperty('getState')).toBe(true) - }) - - it('should return the slice state data', () => { - expect(selectors.getState(2)).toEqual(2) - }) - }) }) describe('when passing slice', () => { - const { actions, reducer, selectors } = createSlice({ + const { actions, reducer } = createSlice({ reducers: { increment: state => state + 1 }, @@ -78,14 +68,6 @@ describe('createSlice', () => { it('should return the correct value from reducer', () => { expect(reducer(undefined, actions.increment())).toEqual(1) }) - - it('should create selector with correct name', () => { - expect(selectors.hasOwnProperty('getCool')).toBe(true) - }) - - it('should return the slice state data', () => { - expect(selectors.getCool({ cool: 2 })).toEqual(2) - }) }) describe('when mutating state object', () => { diff --git a/src/createSlice.ts b/src/createSlice.ts index 0a3f4f5cae..09499d393e 100644 --- a/src/createSlice.ts +++ b/src/createSlice.ts @@ -8,7 +8,6 @@ import { ActionCreatorWithPreparedPayload } from './createAction' import { createReducer, CaseReducers, CaseReducer } from './createReducer' -import { createSliceSelector, createSelectorName } from './sliceSelector' /** * An action creator atttached to a slice. @@ -36,14 +35,6 @@ export interface Slice< * reducer. */ actions: ActionCreators - - /** - * Selectors for the slice reducer state. `createSlice()` inserts a single - * selector that returns the entire slice state and whose name is - * automatically derived from the slice name (e.g., `getCounter` for a slice - * named `counter`). - */ - selectors: { [key: string]: (state: any) => State } } /** @@ -54,8 +45,7 @@ export interface CreateSliceOptions< CR extends SliceCaseReducers = SliceCaseReducers > { /** - * The slice's name. Used to namespace the generated action types and to - * name the selector for retrieving the reducer's state. + * The slice's name. Used to namespace the generated action types. */ slice?: string @@ -156,7 +146,7 @@ function getType(slice: string, actionKey: string): string { /** * 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 + * action creators and action types that correspond to the * reducers and state. * * The `reducer` argument is passed to `createReducer()`. @@ -205,14 +195,9 @@ export function createSlice< {} as any ) - const selectors = { - [createSelectorName(slice)]: createSliceSelector(slice) - } - return { slice, reducer, - actions: actionMap, - selectors + actions: actionMap } } diff --git a/src/sliceSelector.test.ts b/src/sliceSelector.test.ts deleted file mode 100644 index 8df49698bf..0000000000 --- a/src/sliceSelector.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { createSelectorName } from './sliceSelector' - -describe('createSelectorName', () => { - it('should convert to camel case', () => { - expect(createSelectorName('some')).toEqual('getSome') - expect(createSelectorName('someThing')).toEqual('getSomeThing') - expect(createSelectorName('some-thing')).toEqual('getSomeThing') - }) -}) diff --git a/src/sliceSelector.ts b/src/sliceSelector.ts deleted file mode 100644 index 79813ef6ce..0000000000 --- a/src/sliceSelector.ts +++ /dev/null @@ -1,30 +0,0 @@ -export type Selector = (state: S) => R - -export function createSliceSelector(): Selector -export function createSliceSelector< - S extends { [key: string]: any } = any, - R = any ->(slice: string): Selector - -export function createSliceSelector(slice?: string) { - if (!slice) { - return (state: S): S => state - } - return (state: { [key: string]: any }): R => state[slice] -} - -export function createSelectorName(slice: string): string { - if (!slice) { - return 'getState' - } - return camelize(`get ${slice}`) -} - -function camelize(str: string): string { - return str - .replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => { - return index === 0 ? letter.toLowerCase() : letter.toUpperCase() - }) - .replace(/\s+/g, '') - .replace(/[-_]/g, '') -} diff --git a/type-tests/files/createSlice.typetest.ts b/type-tests/files/createSlice.typetest.ts index 5c500a2918..d741a0f822 100644 --- a/type-tests/files/createSlice.typetest.ts +++ b/type-tests/files/createSlice.typetest.ts @@ -40,13 +40,6 @@ function expectType(t: T) { // typings:expect-error slice.actions.other(1) - - /* Selector */ - - const value: number = slice.selectors.getCounter(0) - - // typings:expect-error - const stringValue: string = slice.selectors.getCounter(0) } /*