v2.0.0-beta.0 #3493
Replies: 4 comments 20 replies
-
RTK 2 is looking really good! Looking forward to the final release. Small question: Why was Dictionary removed? Found it quite handy. I can of course inline it in the app, but was wondering if it was considered harmful in any way. |
Beta Was this translation helpful? Give feedback.
-
When I run |
Beta Was this translation helpful? Give feedback.
-
I'm trying to add middleware to the default middleware like this (using typescript):
Everything works as expected, however i'm getting a typescript error for
Should I be using Tuple for this? |
Beta Was this translation helpful? Give feedback.
-
I came up with this 1 year ago :)) Glad it is going to be supported in the main library and I don't have to maintain the codebase for long :D These were my junk takes on this matter:
Hopefully the extrareducers can merge as well. |
Beta Was this translation helpful? Give feedback.
-
This beta release updates many of our TS types for improved type safety and behavior, updates
entityAdapter.getSelectors()
to accept acreateSelector
option, depends on the latest[email protected]
release, and includes all prior changes from the 2.0 alphas. This release has breaking changes.Changelog
Store Configuration Tweaks and Type Safety
We've seen many cases where users passing the
middleware
parameter toconfigureStore
have tried spreading the array returned bygetDefaultMiddleware()
, 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 asdispatch
being typed asDispatch<AnyAction>
and not knowing about thunks).getDefaultMiddleware()
already used an internalMiddlewareArray
class, anArray
subclass that had strongly typed.concat/prepend()
methods to correctly capture and retain the middleware types.We've renamed that type to
Tuple
, andconfigureStore
's TS types now require that you must useTuple
if you want to pass your own array of middleware:(Note that this has no effect if you're using RTK with plain JS, and you could still pass a plain array here.)
Similarly, the
enhancers
field used to accept an array directly. It now is a callback that receives agetDefaultEnhancers
method, equivalent togetDefaultMiddleware()
:It too expects a
Tuple
return value if you're using TS.Entity Adapter Updates
entityAdapter.getSelectors()
now accepts an options object as its second argument. This allows you to pass in your own preferredcreateSelector
method, which will be used to memoize the generated selectors. This could be useful if you want to use one of Reselect's new alternate memoizers, or some other memoization library with an equivalent signature.createEntityAdapter
now accepts anId
generic argument, which will be used to strongly type the item IDs anywhere those are exposed.The
.entities
lookup table is now defined to use a standard TSRecord<Id, MyEntityType>
, which assumes that each item lookup exists by default. Previously, it used aDictionary<MyEntityType>
type, which assumed the result wasMyEntityType | undefined
. TheDictionary
type has been removed.If you prefer to assume that the lookups might be undefined, use TypeScript's
noUncheckedIndexedAccess
configuration option to control that.New
UnknownAction
TypeThe Redux core TS types have always exported an
AnyAction
type, which is defined to have{type: string}
and treat any other field asany
. This makes it easy to write uses likeconsole.log(action.whatever)
, but unfortunately does not provide any meaningful type safety.We now export an
UnknownAction
type, which treats all fields other thanaction.type
asunknown
. This encourages users to write type guards that check the action object and assert its specific TS type. Inside of those checks, you can access a field with better type safety.UnknownAction
is now the default any place in the Redux and RTK source that expects an action object.AnyAction
still exists for compatibility, but has been marked as deprecated.Note that Redux Toolkit's action creators have a
.match()
method that acts as a useful type guard:Earlier Alpha Changes
Summarizing the changes from earlier alphas:
New Features
combineSlices
API with built-in support for slice reducer injection for code-splittingselectors
field increateSlice
createSlice.reducers
, which allows defining thunks insidecreateSlice
configureStore
addsautoBatchEnhancer
by defaultBreaking Changes
createReducer
andcreateSlice.extraReducers
has been removed5.0-beta
actionCreator.toString()
override removed (although we're reconsidering this)getDefaultMiddleware
removedWhat's Changed
createSelector
instance to adapter.getSelectors by @EskiMojo14 in Allow passing acreateSelector
instance to adapter.getSelectors #3481Full Changelog: v2.0.0-alpha.6...v2.0.0-beta.0
This discussion was created from the release v2.0.0-beta.0.
Beta Was this translation helpful? Give feedback.
All reactions