npm @reduxjs/toolkit 1.1.0
v1.1.0

latest releases: 2.2.3, 2.2.2, 2.2.1...
4 years ago

This release adds a utility function for better type safety with reducer object parameters, and fixes an issue with error message in the serializability check middleware.

Changes

Type-Safe Reducer Object Builder API

createReducer accepts a plain object full of reducer functions as a parameter, where the keys are the action types that should be handled. While this works fine with plain JS, TypeScript is unable to infer the correct type for the action parameters in each reducer.

As an alternative, you may now pass a callback function to createReducer that will be given a "builder" object that allows you to add reducers in a type-safe way based on the provided action types:

const increment = createAction<number, 'increment'>('increment')
const decrement = createAction<number, 'decrement'>('decrement')
createReducer(0, builder =>
  builder
    .addCase(increment, (state, action) => {
      // action is inferred correctly here
    })
    .addCase(decrement, (state, action: PayloadAction<string>) => {
      // this would error out
    })
)

While this API is usable from plain JS, it has no real benefit there, and is primarily intended for use with TS.

The same API is also available for the extraReducers argument of createSlice. It is not necessary for the reducers argument, as the action types are already being defined there.

Serialization Error Fixes

Error messages for the serialization check middleware were not correctly displaying the value. This has been fixed.

Docs Updates

Docusaurus v2

Our documentation site at https://redux-toolkit.js.org has been upgraded to use Docusaurus v2! This comes with a shiny new look and feel, and page loads are now even more Blazing Fast (TM).

Thanks to @endiliey, @yangshunz, @wgao19, and @ashakkk for all their hard work on the migration!

New "Usage with TypeScript" Page

We now have a new "Usage with TypeScript" docs page that has examples on how to correctly write and type usage of RTK.

Changelog

Code

Docs

v1.0.4...v1.1.0

Don't miss a new toolkit release

NewReleases is sending notifications on new releases.