Skip to content

Commit 5193835

Browse files
authored
Merge pull request #4473 from reduxjs/feature/5.0-createStore-deprecation
2 parents 6294ad2 + 37350b2 commit 5193835

File tree

2 files changed

+140
-58
lines changed

2 files changed

+140
-58
lines changed

src/createStore.ts

Lines changed: 138 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,65 @@ import isPlainObject from './utils/isPlainObject'
1515
import { kindOf } from './utils/kindOf'
1616

1717
/**
18-
* Creates a Redux store that holds the state tree.
19-
* The only way to change the data in the store is to call `dispatch()` on it.
18+
* @deprecated
2019
*
21-
* There should only be a single store in your app. To specify how different
22-
* parts of the state tree respond to actions, you may combine several reducers
23-
* into a single reducer function by using `combineReducers`.
20+
* **We recommend using the `configureStore` method
21+
* of the `@reduxjs/toolkit` package**, which replaces `createStore`.
2422
*
25-
* @param reducer A function that returns the next state tree, given
26-
* the current state tree and the action to handle.
23+
* Redux Toolkit is our recommended approach for writing Redux logic today,
24+
* including store setup, reducers, data fetching, and more.
2725
*
28-
* @param preloadedState The initial state. You may optionally specify it
29-
* to hydrate the state from the server in universal apps, or to restore a
30-
* previously serialized user session.
31-
* If you use `combineReducers` to produce the root reducer function, this must be
32-
* an object with the same shape as `combineReducers` keys.
26+
* **For more details, please read this Redux docs page:**
27+
* **https://redux.js.org/introduction/why-rtk-is-redux-today**
3328
*
34-
* @param enhancer The store enhancer. You may optionally specify it
35-
* to enhance the store with third-party capabilities such as middleware,
36-
* time travel, persistence, etc. The only store enhancer that ships with Redux
37-
* is `applyMiddleware()`.
29+
* `configureStore` from Redux Toolkit is an improved version of `createStore` that
30+
* simplifies setup and helps avoid common bugs.
31+
*
32+
* You should not be using the `redux` core package by itself today, except for learning purposes.
33+
* The `createStore` method from the core `redux` package will not be removed, but we encourage
34+
* all users to migrate to using Redux Toolkit for all Redux code.
35+
*
36+
* If you want to use `createStore` without this visual deprecation warning, use
37+
* the `legacy_createStore` import instead:
38+
*
39+
* `import { legacy_createStore as createStore} from 'redux'`
3840
*
39-
* @returns A Redux store that lets you read the state, dispatch actions
40-
* and subscribe to changes.
4141
*/
42-
export default function createStore<
43-
S,
44-
A extends Action,
45-
Ext = {},
46-
StateExt = never
47-
>(
42+
export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
4843
reducer: Reducer<S, A>,
4944
enhancer?: StoreEnhancer<Ext, StateExt>
5045
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
51-
export default function createStore<
52-
S,
53-
A extends Action,
54-
Ext = {},
55-
StateExt = never
56-
>(
46+
/**
47+
* @deprecated
48+
*
49+
* **We recommend using the `configureStore` method
50+
* of the `@reduxjs/toolkit` package**, which replaces `createStore`.
51+
*
52+
* Redux Toolkit is our recommended approach for writing Redux logic today,
53+
* including store setup, reducers, data fetching, and more.
54+
*
55+
* **For more details, please read this Redux docs page:**
56+
* **https://redux.js.org/introduction/why-rtk-is-redux-today**
57+
*
58+
* `configureStore` from Redux Toolkit is an improved version of `createStore` that
59+
* simplifies setup and helps avoid common bugs.
60+
*
61+
* You should not be using the `redux` core package by itself today, except for learning purposes.
62+
* The `createStore` method from the core `redux` package will not be removed, but we encourage
63+
* all users to migrate to using Redux Toolkit for all Redux code.
64+
*
65+
* If you want to use `createStore` without this visual deprecation warning, use
66+
* the `legacy_createStore` import instead:
67+
*
68+
* `import { legacy_createStore as createStore} from 'redux'`
69+
*
70+
*/
71+
export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
5772
reducer: Reducer<S, A>,
5873
preloadedState?: PreloadedState<S>,
5974
enhancer?: StoreEnhancer<Ext, StateExt>
6075
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
61-
export default function createStore<
62-
S,
63-
A extends Action,
64-
Ext = {},
65-
StateExt = never
66-
>(
76+
export function createStore<S, A extends Action, Ext = {}, StateExt = never>(
6777
reducer: Reducer<S, A>,
6878
preloadedState?: PreloadedState<S> | StoreEnhancer<Ext, StateExt>,
6979
enhancer?: StoreEnhancer<Ext, StateExt>
@@ -367,3 +377,95 @@ export default function createStore<
367377
} as unknown as Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
368378
return store
369379
}
380+
381+
/**
382+
* Creates a Redux store that holds the state tree.
383+
*
384+
* **We recommend using `configureStore` from the
385+
* `@reduxjs/toolkit` package**, which replaces `createStore`:
386+
* **https://redux.js.org/introduction/why-rtk-is-redux-today**
387+
*
388+
* The only way to change the data in the store is to call `dispatch()` on it.
389+
*
390+
* There should only be a single store in your app. To specify how different
391+
* parts of the state tree respond to actions, you may combine several reducers
392+
* into a single reducer function by using `combineReducers`.
393+
*
394+
* @param {Function} reducer A function that returns the next state tree, given
395+
* the current state tree and the action to handle.
396+
*
397+
* @param {any} [preloadedState] The initial state. You may optionally specify it
398+
* to hydrate the state from the server in universal apps, or to restore a
399+
* previously serialized user session.
400+
* If you use `combineReducers` to produce the root reducer function, this must be
401+
* an object with the same shape as `combineReducers` keys.
402+
*
403+
* @param {Function} [enhancer] The store enhancer. You may optionally specify it
404+
* to enhance the store with third-party capabilities such as middleware,
405+
* time travel, persistence, etc. The only store enhancer that ships with Redux
406+
* is `applyMiddleware()`.
407+
*
408+
* @returns {Store} A Redux store that lets you read the state, dispatch actions
409+
* and subscribe to changes.
410+
*/
411+
export function legacy_createStore<
412+
S,
413+
A extends Action,
414+
Ext = {},
415+
StateExt = never
416+
>(
417+
reducer: Reducer<S, A>,
418+
enhancer?: StoreEnhancer<Ext, StateExt>
419+
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
420+
/**
421+
* Creates a Redux store that holds the state tree.
422+
*
423+
* **We recommend using `configureStore` from the
424+
* `@reduxjs/toolkit` package**, which replaces `createStore`:
425+
* **https://redux.js.org/introduction/why-rtk-is-redux-today**
426+
*
427+
* The only way to change the data in the store is to call `dispatch()` on it.
428+
*
429+
* There should only be a single store in your app. To specify how different
430+
* parts of the state tree respond to actions, you may combine several reducers
431+
* into a single reducer function by using `combineReducers`.
432+
*
433+
* @param {Function} reducer A function that returns the next state tree, given
434+
* the current state tree and the action to handle.
435+
*
436+
* @param {any} [preloadedState] The initial state. You may optionally specify it
437+
* to hydrate the state from the server in universal apps, or to restore a
438+
* previously serialized user session.
439+
* If you use `combineReducers` to produce the root reducer function, this must be
440+
* an object with the same shape as `combineReducers` keys.
441+
*
442+
* @param {Function} [enhancer] The store enhancer. You may optionally specify it
443+
* to enhance the store with third-party capabilities such as middleware,
444+
* time travel, persistence, etc. The only store enhancer that ships with Redux
445+
* is `applyMiddleware()`.
446+
*
447+
* @returns {Store} A Redux store that lets you read the state, dispatch actions
448+
* and subscribe to changes.
449+
*/
450+
export function legacy_createStore<
451+
S,
452+
A extends Action,
453+
Ext = {},
454+
StateExt = never
455+
>(
456+
reducer: Reducer<S, A>,
457+
preloadedState?: PreloadedState<S>,
458+
enhancer?: StoreEnhancer<Ext, StateExt>
459+
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext
460+
export function legacy_createStore<
461+
S,
462+
A extends Action,
463+
Ext = {},
464+
StateExt = never
465+
>(
466+
reducer: Reducer<S, A>,
467+
preloadedState?: PreloadedState<S> | StoreEnhancer<Ext, StateExt>,
468+
enhancer?: StoreEnhancer<Ext, StateExt>
469+
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext {
470+
return createStore(reducer, preloadedState as any, enhancer)
471+
}

src/index.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// functions
2-
import createStore from './createStore'
2+
import { createStore, legacy_createStore } from './createStore'
33
import combineReducers from './combineReducers'
44
import bindActionCreators from './bindActionCreators'
55
import applyMiddleware from './applyMiddleware'
66
import compose from './compose'
7-
import warning from './utils/warning'
87
import __DO_NOT_USE__ActionTypes from './utils/actionTypes'
98

109
// types
@@ -38,28 +37,9 @@ export { MiddlewareAPI, Middleware } from './types/middleware'
3837
// actions
3938
export { Action, AnyAction } from './types/actions'
4039

41-
/*
42-
* This is a dummy function to check if the function name has been altered by minification.
43-
* If the function has been minified and NODE_ENV !== 'production', warn the user.
44-
*/
45-
function isCrushed() {}
46-
47-
if (
48-
process.env.NODE_ENV !== 'production' &&
49-
typeof isCrushed.name === 'string' &&
50-
isCrushed.name !== 'isCrushed'
51-
) {
52-
warning(
53-
'You are currently using minified code outside of NODE_ENV === "production". ' +
54-
'This means that you are running a slower development build of Redux. ' +
55-
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
56-
'or setting mode to production in webpack (https://webpack.js.org/configuration/mode/) ' +
57-
'to ensure you have the correct code for your production build.'
58-
)
59-
}
60-
6140
export {
6241
createStore,
42+
legacy_createStore,
6343
combineReducers,
6444
bindActionCreators,
6545
applyMiddleware,

0 commit comments

Comments
 (0)