Skip to content

Commit 572675a

Browse files
authored
Merge pull request #3936 from reduxjs/docs/2.0-migration-polish
2 parents 2ddabcc + b91595c commit 572675a

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

docs/migrations/1.x-to-2.x.md renamed to docs/usage/migrating-rtk-2.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
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
55
hide_title: true
66
toc_max_heading_level: 4
77
---
@@ -10,21 +10,21 @@ toc_max_heading_level: 4
1010

1111
<div className="migration-guide">
1212

13-
# Migrating 1.x → 2.x
13+
# Migrating to RTK 2.0 and Redux 5.0
1414

1515
:::tip What You'll Learn
1616

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
1818

1919
:::
2020

2121
## Introduction
2222

2323
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.
2424

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**.
2626

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.
2828

2929
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**.
3030

@@ -49,7 +49,7 @@ We've done local testing of the package, but we ask the community to try out thi
4949
We've updated the build output in several ways:
5050

5151
- **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
5353
- The lowest Typescript version we test against is now 4.7
5454

5555
#### Dropping UMD builds
@@ -94,7 +94,7 @@ To fix this, there are three options:
9494

9595
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).
9696

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).
9898

9999
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.
100100

@@ -339,6 +339,10 @@ Redux 4.1.0 optimized its bundle size by [extracting error message strings out o
339339

340340
<div class="typescript-only">
341341

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+
342346
#### Non-default middleware/enhancers must use `Tuple`
343347

344348
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).
@@ -352,7 +356,7 @@ import { configureStore, Tuple } from '@reduxjs/toolkit'
352356

353357
configureStore({
354358
reducer: rootReducer,
355-
middleware: (getDefaultMidldeware) => new Tuple(additionalMiddleware, logger),
359+
middleware: (getDefaultMiddleware) => new Tuple(additionalMiddleware, logger),
356360
})
357361
```
358362

@@ -362,14 +366,63 @@ This same restriction applies to the `enhancers` field.
362366

363367
#### Entity adapter type updates
364368

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!**
366370

367371
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.
368372

369373
If you prefer to assume that the lookups _might_ be undefined, use TypeScript's `noUncheckedIndexedAccess` configuration option to control that.
370374

371375
</div>
372376

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:
384+
385+
```ts no-emit
386+
createSelector(inputs, resultFn, {
387+
memoize: lruMemoize,
388+
memoizeOptions: { equalityCheck: yourEqualityFunction },
389+
})
390+
```
391+
392+
#### `defaultMemoize` Renamed to `lruMemoize`
393+
394+
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+
<div class="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+
373426
## New Features
374427

375428
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.

website/_redirects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
# Relocated content
1313
/rtk-query/usage/optimistic-updates /rtk-query/usage/manual-cache-updates#optimistic-updates
1414
/rtk-query/api/created-api/cache-management-utils /rtk-query/api/created-api/api-slice-utils
15+
/migrations/migrating-1.x-to-2.x /usage/migrating-rtk-2

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{
2727
"type": "category",
2828
"label": "Migrations",
29-
"items": ["migrations/migrating-1.x-to-2.x"]
29+
"items": ["usage/migrating-rtk-2"]
3030
},
3131
"usage/usage-guide",
3232
"usage/usage-with-typescript",

0 commit comments

Comments
 (0)