v1.8.0 #2080
markerikson
announced in
Announcements
v1.8.0
#2080
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This release adds the new "listener" middleware, updates
configureStore
's types to better handle type inference from middleware that overridedispatch
return values, and updates our TS support matrix to drop support for TS < 4.1.Changelog
New "Listener" Side Effects Middleware
RTK has integrated the thunk middleware since the beginning. However, thunks are imperative functions, and do not let you run code in response to dispatched actions. That use case has typically been covered with libraries like
redux-saga
(which handles side effects with "sagas" based on generator functions),redux-observable
(which uses RxJS observables), or custom middleware.We've added a new "listener" middleware to RTK to cover that use case. The listener middleware is created using
createListenerMiddleware()
, and lets you define "listener" entries that contain an "effect" callback with additional logic and a way to specify when that callback should run based on dispatched actions or state changes.Conceptually, you can think of this as being similar to React's
useEffect
hook, except that it runs logic in response to Redux store updates instead of component props/state updates.The listener middleware is intended to be a lightweight alternative to more widely used Redux async middleware like sagas and observables. While similar to thunks in level of complexity and concept, it can replicate some common saga usage patterns. We believe that the listener middleware can be used to replace most of the remaining use cases for sagas, but with a fraction of the bundle size and a much simpler API.
Listener effect callbacks have access to
dispatch
andgetState
, similar to thunks. The listener also receives a set of async workflow functions liketake
,condition
,pause
,fork
, andunsubscribe
, which allow writing more complex async logic.Listeners can be defined statically by calling
listenerMiddleware.startListening()
during setup, or added and removed dynamically at runtime with specialdispatch(addListener())
anddispatch(removeListener())
actions.The API reference is available at:
https://redux-toolkit.js.org/api/createListenerMiddleware
Huge thanks to @FaberVitale for major contributions in refining the middleware API and implementing key functionality.
Basic usage of the listener middleware looks like:
You can use it to write more complex async workflows, including pausing the effect callback until a condition check resolves, and forking "child tasks" to do additional work:
configureStore
Middleware Type ImprovementsMiddleware can override the default return value of
dispatch
.configureStore
tries to extract any declareddispatch
type overrides from themiddleware
array, and uses that to alter the type ofstore.dispatch
.We identified some cases where the type inference wasn't working well enough, and rewrote the type behavior to be more correct.
TypeScript Support Matrix Updates
RTK now requires TS 4.1 or greater to work correctly, and we've dropped 4.0 and earlier from our support matrix.
Other Changes
The internal logc for the serializability middleware has been reorganized to allow skipping checks against actions, while still checking values in the state.
What's Changed
Since most of the implementation work on the middleware was done over the last few months, this list only contains the most recent PRs since 1.7.2. For details on the original use case discussions and the evolution of the middleware API over time, see:
Full Changelog: v1.7.2...v1.8.0
This discussion was created from the release v1.8.0.
Beta Was this translation helpful? Give feedback.
All reactions