Using Existing Middleware Libraries
When it comes to using middleware in a Redux application, there are several existing libraries available that provide pre-built middleware functions for common use cases. These middleware libraries can greatly simplify the process of adding middleware to your Redux store.
One popular middleware library is redux-thunk
. This library allows you to write action creators that return functions instead of plain action objects. These functions can then be used to perform asynchronous operations or dispatch multiple actions.
Here is an example of how to use redux-thunk
as middleware in a Redux store:
JAVASCRIPT
1import thunk from 'redux-thunk';
2import { applyMiddleware, createStore } from 'redux';
3
4const store = createStore(reducer, applyMiddleware(thunk));
xxxxxxxxxx
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import { applyMiddleware, createStore } from 'redux';
const loggerMiddleware = createLogger();
const store = createStore(reducer, applyMiddleware(thunk, loggerMiddleware));
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment