Skip to content

Commit d667d8b

Browse files
committed
Remove legacy object syntax for reducers
1 parent 496fe5c commit d667d8b

File tree

6 files changed

+118
-277
lines changed

6 files changed

+118
-277
lines changed

packages/toolkit/src/createReducer.ts

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -151,91 +151,20 @@ export function createReducer<S extends NotFunction<any>>(
151151
builderCallback: (builder: ActionReducerMapBuilder<S>) => void
152152
): ReducerWithInitialState<S>
153153

154-
/**
155-
* A utility function that allows defining a reducer as a mapping from action
156-
* type to *case reducer* functions that handle these action types. The
157-
* reducer's initial state is passed as the first argument.
158-
*
159-
* The body of every case reducer is implicitly wrapped with a call to
160-
* `produce()` from the [immer](https://github.com/mweststrate/immer) library.
161-
* This means that rather than returning a new state object, you can also
162-
* mutate the passed-in state object directly; these mutations will then be
163-
* automatically and efficiently translated into copies, giving you both
164-
* convenience and immutability.
165-
*
166-
* @overloadSummary
167-
* This overload accepts an object where the keys are string action types, and the values
168-
* are case reducer functions to handle those action types.
169-
*
170-
* @param initialState - `State | (() => State)`: The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
171-
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
172-
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
173-
* All matching reducers will be executed in order, independently if a case reducer matched or not.
174-
* @param defaultCaseReducer - A "default case" reducer that is executed if no case reducer and no matcher
175-
* reducer was executed for this action.
176-
*
177-
* @example
178-
```js
179-
const counterReducer = createReducer(0, {
180-
increment: (state, action) => state + action.payload,
181-
decrement: (state, action) => state - action.payload
182-
})
183-
184-
// Alternately, use a "lazy initializer" to provide the initial state
185-
// (works with either form of createReducer)
186-
const initialState = () => 0
187-
const counterReducer = createReducer(initialState, {
188-
increment: (state, action) => state + action.payload,
189-
decrement: (state, action) => state - action.payload
190-
})
191-
```
192-
193-
* Action creators that were generated using [`createAction`](./createAction) may be used directly as the keys here, using computed property syntax:
194-
195-
```js
196-
const increment = createAction('increment')
197-
const decrement = createAction('decrement')
198-
199-
const counterReducer = createReducer(0, {
200-
[increment]: (state, action) => state + action.payload,
201-
[decrement.type]: (state, action) => state - action.payload
202-
})
203-
```
204-
* @public
205-
*/
206-
export function createReducer<
207-
S extends NotFunction<any>,
208-
CR extends CaseReducers<S, any> = CaseReducers<S, any>
209-
>(
210-
initialState: S | (() => S),
211-
actionsMap: CR,
212-
actionMatchers?: ActionMatcherDescriptionCollection<S>,
213-
defaultCaseReducer?: CaseReducer<S>
214-
): ReducerWithInitialState<S>
215-
216154
export function createReducer<S extends NotFunction<any>>(
217155
initialState: S | (() => S),
218-
mapOrBuilderCallback:
219-
| CaseReducers<S, any>
220-
| ((builder: ActionReducerMapBuilder<S>) => void),
221-
actionMatchers: ReadonlyActionMatcherDescriptionCollection<S> = [],
222-
defaultCaseReducer?: CaseReducer<S>
156+
mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void
223157
): ReducerWithInitialState<S> {
224158
if (process.env.NODE_ENV !== 'production') {
225159
if (typeof mapOrBuilderCallback === 'object') {
226-
if (!hasWarnedAboutObjectNotation) {
227-
hasWarnedAboutObjectNotation = true
228-
console.warn(
229-
"The object notation for `createReducer` is deprecated, and will be removed in RTK 2.0. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer"
230-
)
231-
}
160+
throw new Error(
161+
"The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer"
162+
)
232163
}
233164
}
234165

235166
let [actionsMap, finalActionMatchers, finalDefaultCaseReducer] =
236-
typeof mapOrBuilderCallback === 'function'
237-
? executeReducerBuilderCallback(mapOrBuilderCallback)
238-
: [mapOrBuilderCallback, actionMatchers, defaultCaseReducer]
167+
executeReducerBuilderCallback(mapOrBuilderCallback)
239168

240169
// Ensure the initial state gets frozen either way (if draftable)
241170
let getInitialState: () => S

packages/toolkit/src/createSlice.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ createSlice({
141141
})
142142
```
143143
*/
144-
extraReducers?:
145-
| CaseReducers<NoInfer<State>, any>
146-
| ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void)
144+
extraReducers?: (builder: ActionReducerMapBuilder<NoInfer<State>>) => void
147145
}
148146

149147
/**
@@ -330,12 +328,9 @@ export function createSlice<
330328
function buildReducer() {
331329
if (process.env.NODE_ENV !== 'production') {
332330
if (typeof options.extraReducers === 'object') {
333-
if (!hasWarnedAboutObjectNotation) {
334-
hasWarnedAboutObjectNotation = true
335-
console.warn(
336-
"The object notation for `createSlice.extraReducers` is deprecated, and will be removed in RTK 2.0. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice"
337-
)
338-
}
331+
throw new Error(
332+
"The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice"
333+
)
339334
}
340335
}
341336
const [

0 commit comments

Comments
 (0)