concept thunk in category react

This is an excerpt from Manning's book Redux in Action.
Open index.js, import the logger, and add it to the argument list for applyMiddleware alongside thunk, as shown in listing 5.2. Whenever you want to add a new middleware to your applications, you’ll follow this process. Note that you add the logger middleware last, and they run in the order that you pass them to applyMiddleware. It’s not critical in this case, that the middleware be in any particular order, but you should be aware of the significance of their order.
Any tool has tradeoffs, of course. The simplicity of thunks makes them something of a double-edged sword. Thunks are easy to write, but you’re on your own to write advanced functionality.
After configuring the store to use saga middleware, you instructed the middleware to run the root saga. Because it’s simpler to keep track of a single entry point, the root saga’s role will be to coordinate all other sagas used in the application. When that root saga is implemented, you’ll expect it to kick off sagas to run in the background, watching for and reacting to actions of specific types. As mentioned, you can and will use thunks at the same time, so each saga will listen for and respond to only specific actions. See figure 6.2 for a visual representation.
You’ve had great exposure to thunks already, having used them to interact with a remote server to fetch a list of tasks or create a new one. To review, the Redux store and its reducers know what to do with actions. Actions describe events—opening a modal, for example, can be the result of a clicked button dispatching an action of type OPEN_MODAL.
Listing 6.1. src/actions/index.js
export function fetchTasks() { return (dispatch, getState) => { #1 dispatch(fetchTasksRequest()); #2 ... dispatch(fetchTasksSuccess()); #3 ... } } #1 The action creator returns a function, also known as a thunk. #2 Within the thunk, more action creators can be dispatched. #3 Based on the results of a side effect, more dispatching may occur. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":43},{\"line\":1,\"ch\":45}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":2,\"ch\":43},{\"line\":2,\"ch\":45}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":4,\"ch\":43},{\"line\":4,\"ch\":45}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}]]"} !@%STYLE%@!