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/WritingCustomMiddleware.md
+64-63
Original file line number
Diff line number
Diff line change
@@ -39,74 +39,75 @@ You might still want to use custom middleware in one of two cases:
39
39
### Create side effects for actions
40
40
41
41
This is the most common middleware. Here's what it looks like for [rtk listener middleware](https://github.com/reduxjs/redux-toolkit/blob/0678c2e195a70c34cd26bddbfd29043bc36d1362/packages/toolkit/src/listenerMiddleware/index.ts#L427):
In the first part, it listens to `addListener`, `clearAllListeners` and `removeListener` actions to change which listeners should be invoked later on.
@@ -120,20 +121,20 @@ It is common to have side effects after dispatching th eaction, because this all
120
121
While these patterns are less common, most of them (except for cancelling actions) are used by [redux thunk middleware](https://github.com/reduxjs/redux-thunk/blob/587a85b1d908e8b7cf2297bec6e15807d3b7dc62/src/index.ts#L22):
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
129
+
// If this "action" is really a function, call it and return the result.
130
+
if (typeofaction==='function') {
131
+
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
132
+
returnaction(dispatch, getState, extraArgument)
136
133
}
134
+
135
+
// Otherwise, pass the action down the middleware chain as usual
136
+
returnnext(action)
137
+
}
137
138
```
138
139
139
140
Usually, `dispatch` can only handle JSON actions. This middleware adds the ability to also handle actions in the form of functions. It also changes the return type of the dispatch function itself by passing the return value of the function-action to be the return value of the dispatch function.
0 commit comments