Adjusting the store

In order for us to be able to use asynchronous action creator functions in Redux, we are going to need to load the redux-thunk middleware. This middleware checks if an action creator returned a function, rather than a plain object, and if that is the case, it executes that function, while passing the dispatch function to it as an argument.

Let's adjust the store to allow for asynchronous action creators now:

  1. Create a new src/configureStore.js file.
  2. Import the createStore and applyMiddleware functions from Redux:
import { createStore, applyMiddleware } from 'redux'
  1. Next, import the thunk middleware and appReducer function:
import thunk from 'redux-thunk'

import appReducer from './reducers'
  1. Now, we can define the store and apply the thunk middleware to it:
const store = createStore(appReducer, applyMiddleware(thunk))
  1. Finally, we export the store:
export default store

Using the redux-thunk middleware, we can now dispatch functions that will later dispatch actions, which means that our asynchronous action creator is going to work fine now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.70.203