You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage/migrating-rtk-2.md
+64-11Lines changed: 64 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
-
id: migrating-1.x-to-2.x
3
-
title: Migrating 1.x → 2.x
4
-
sidebar_label: Migrating 1.x → 2.x
2
+
id: migrating-rtk-2
3
+
title: Migrating to RTK 2.0 and Redux 5.0
4
+
sidebar_label: Migrating to RTK 2.0 and Redux 5.0
5
5
hide_title: true
6
6
toc_max_heading_level: 4
7
7
---
@@ -10,21 +10,21 @@ toc_max_heading_level: 4
10
10
11
11
<divclassName="migration-guide">
12
12
13
-
# Migrating 1.x → 2.x
13
+
# Migrating to RTK 2.0 and Redux 5.0
14
14
15
15
:::tip What You'll Learn
16
16
17
-
- What's changed in Redux Toolkit 2.0 and Redux core 5.0, including breaking changes and new features
17
+
- What's changed in Redux Toolkit 2.0, Redux core 5.0, Reselect 5.0, and Redux Thunk 3.0, including breaking changes and new features
18
18
19
19
:::
20
20
21
21
## Introduction
22
22
23
23
Redux Toolkit has been available since 2019, and today it's the standard way to write Redux apps. We've gone 4+ years without any breaking changes. Now, RTK 2.0 gives us a chance to modernize the packaging, clean up deprecated options, and tighten up some edge cases.
24
24
25
-
Redux Toolkit 2.0 is accompanied by major versions of all the other Redux packages: Redux core 5.0, React-Redux 9.0, Redux Thunk 3.0, and Reselect 5.0.
25
+
**Redux Toolkit 2.0 is accompanied by major versions of all the other Redux packages: Redux core 5.0, React-Redux 9.0, Reselect 5.0, and Redux Thunk 3.0**.
26
26
27
-
This page lists known potentially breaking changes in Redux Toolkit 2.0 and Redux core 5.0, as well as new features in Redux Toolkit 2.0. As a reminder, **you should not need to actually install or use the core `redux` package directly** - RTK wraps that, and re-exports all methods and types.
27
+
This page lists known potentially breaking changes in each of those packages, as well as new features in Redux Toolkit 2.0. As a reminder, **you should not need to actually install or use the core `redux` package directly** - RTK wraps that, and re-exports all methods and types.
28
28
29
29
In practice, **most of the "breaking" changes should not have an actual effect on end users, and we expect that many projects can just update the package versions with very few code changes needed**.
30
30
@@ -49,7 +49,7 @@ We've done local testing of the package, but we ask the community to try out thi
49
49
We've updated the build output in several ways:
50
50
51
51
-**Build output is no longer transpiled!** Instead we target modern JS syntax (ES2020)
52
-
- Moved all build artifacts to live under ./dist/, instead of separate top-level folders
52
+
- Moved all build artifacts to live under `./dist/`, instead of separate top-level folders
53
53
- The lowest Typescript version we test against is now 4.7
54
54
55
55
#### Dropping UMD builds
@@ -94,7 +94,7 @@ To fix this, there are three options:
94
94
95
95
In 2019, we began a community-powered conversion of the Redux codebase to TypeScript. The original effort was discussed in [#3500: Port to TypeScript](https://github.com/reduxjs/redux/issues/3500), and the work was integrated in PR [#3536: Convert to TypeScript](https://github.com/reduxjs/redux/issues/3536).
96
96
97
-
However, the TS-converted code in master has sat around since then, unused and unpublished, due to concerns about possible compatibility issues with the existing ecosystem (as well as general inertia on our part).
97
+
However, the TS-converted code sat around in the repo for several years, unused and unpublished, due to concerns about possible compatibility issues with the existing ecosystem (as well as general inertia on our part).
98
98
99
99
Redux core v5 is now built from that TS-converted source code. In theory, this should be almost identical in both runtime behavior and types to the 4.x build, but it's very likely that some of the changes may cause types issues.
100
100
@@ -339,6 +339,10 @@ Redux 4.1.0 optimized its bundle size by [extracting error message strings out o
339
339
340
340
<divclass="typescript-only">
341
341
342
+
#### `configureStore` field order for `middleware` matters
343
+
344
+
If you are passing _both_ the `middleware` and `enhancers` fields to `configureStore`, the `middleware` field _must_ come first in order for internal TS inference to work properly.
345
+
342
346
#### Non-default middleware/enhancers must use `Tuple`
343
347
344
348
We've seen many cases where users passing the `middleware` parameter to configureStore have tried spreading the array returned by `getDefaultMiddleware()`, or passed an alternate plain array. This unfortunately loses the exact TS types from the individual middleware, and often causes TS problems down the road (such as `dispatch` being typed as `Dispatch<AnyAction>` and not knowing about thunks).
@@ -362,14 +366,63 @@ This same restriction applies to the `enhancers` field.
362
366
363
367
#### Entity adapter type updates
364
368
365
-
`createEntityAdapter` now has an `Id` generic argument, which will be used to strongly type the item IDs anywhere those are exposed. Previously, the ID field type was always `string | number`. TS will now try to infer the exact type from either the `.id` field of your entity type, or the `selectId` return type. You could also fall back to passing that generic type directly.
369
+
`createEntityAdapter` now has an `Id` generic argument, which will be used to strongly type the item IDs anywhere those are exposed. Previously, the ID field type was always `string | number`. TS will now try to infer the exact type from either the `.id` field of your entity type, or the `selectId` return type. You could also fall back to passing that generic type directly.**If you use the `EntityState<Data, Id>` type directly, you _must_ supply both generic arguments!**
366
370
367
371
The `.entities` lookup table is now defined to use a standard TS `Record<Id, MyEntityType>`, which assumes that each item lookup exists by default. Previously, it used a `Dictionary<MyEntityType>` type, which assumed the result was `MyEntityType | undefined`. The `Dictionary` type has been removed.
368
372
369
373
If you prefer to assume that the lookups _might_ be undefined, use TypeScript's `noUncheckedIndexedAccess` configuration option to control that.
370
374
371
375
</div>
372
376
377
+
### Reselect
378
+
379
+
#### `createSelector` Uses `weakMapMemoize` As Default Memoizer
380
+
381
+
**`createSelector` now uses a new default memoization function called `weakMapMemoize`**. This memoizer offers an effectively infinite cache size, which should simplify usage with varying arguments, but relies exclusively on reference comparisons.
382
+
383
+
If you need to customize equality comparisons, customize `createSelector` to use the original `lruMemoize` method instead:
Since the original `defaultMemoize` function is no longer actually the default, we've renamed it to `lruMemoize` for clarity. This only matters if you specifically imported it into your app to customize selectors.
395
+
396
+
#### `createSelector` Dev-Mode Checks
397
+
398
+
`createSelector` now does checks in development mode for common mistakes, like input selectors that always return new references, or result functions that immediately return their argument. These checks can be customized at selector creation or globally.
399
+
400
+
<divclass="typescript-only">
401
+
402
+
#### `ParametricSelector` Types Removed
403
+
404
+
The `ParametricSelector` and `OutputParametricSelector` types have been removed. Use `Selector` and `OutputSelector` instead.
405
+
406
+
</div>
407
+
408
+
### React-Redux
409
+
410
+
#### Requires React 18
411
+
412
+
React-Redux v7 and v8 worked with all versions of React that supported hooks (16.8+, 17, and 18). v8 switched from internal subscription management to React's new `useSyncExternalStore` hook, but used the "shim" implementation to provide support for React 16.8 and 17, which did not have that hook built in.
413
+
414
+
**React-Redux v9 switches to _requiring_ React 18, and does _not_ support React 16 or 17**. This allows us to drop the shim and save a small bit of bundle size.
415
+
416
+
### Redux Thunk
417
+
418
+
#### Thunk Uses Named Exports
419
+
420
+
The `redux-thunk` package previously used a single default export that was the middleware, with an attached field named `withExtraArgument` that allowed customization.
421
+
422
+
The default export has been removed. There are now two named exports: `thunk` (the basic middleware) and `withExtraArgument`.
423
+
424
+
If you are using Redux Toolkit, this should have no effect, as RTK already handles this inside of `configureStore`.
425
+
373
426
## New Features
374
427
375
428
These features are new in Redux Toolkit 2.0, and help cover additional use cases that we've seen users ask for in the ecosystem.
0 commit comments