Skip to content
No description, website, or topics provided.
JavaScript CSS HTML
Branch: master
Clone or download
Latest commit 34c1d8c Mar 22, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
public tests Mar 18, 2019
src reducer test Mar 21, 2019
.gitignore Initial commit from Create React App Mar 9, 2019 Update Mar 21, 2019
package-lock.json express server to heroku Mar 18, 2019
package.json express server to heroku Mar 18, 2019
server.js express server to heroku Mar 18, 2019

Asynchronous Redux Without Middlewares Middlewares Using ES2017

It is possible to handle asynchronous redux actions without middlewares and, with ES2017 async/await, we can do it in even more clear way.

Should I do it?

I am not here to say that you shouldn't use thunk or saga. If you prefer, you can think of it as an exercise to help us to deepen our knowledge of redux and functional programming. Also, we are going to do it all in a testable way.

The project

We are going to create a simple React project to consume the GitHub API to search repositories by username. If you want to see how the final product looks like, visit the link below:

Let's code

We are going to use create-react-app CLI:

npx create-react-app async-redux-without-middlewares
cd async-redux-without-middlewares

Let's also install the dependencies

npm install -s redux react-redux axios

The http service

Services are created to separate the http requests from the business logic. On top of that, we are going to add an abstraction layer, exporting not the service object, but a function that generates the service object. This allows us to isolate the axios dependency helping us to test the service without having to actually call the http request.


<iframe src="" frameborder=0></iframe>

The action


<iframe src="" frameborder=0></iframe>

The exported function getReposByUsernameInjector is the one that is going to be used to connect() our main container. It will not be referenced, but executed, and its result will become a property of the component.

Receiving all dependencies of getReposByUsernameas parameters, isolates the function, allowing us to write tests without having to import the store or actually calling our service.


In react, containers are components that connects to the redux state. For didactic reasons we are going to separate the connection from the component itself.


<iframe src="" frameborder=0></iframe>


<iframe src="" frameborder=0></iframe>

The second parameter of connect() is a function called mapDispatchToProps, which lets you create functions that dispatch when called, and pass the resulting object properties as props to your component.

If you want to learn more about actions dispatching, I really recommend reading this page from the redux documentation. Connect: Dispatching Actions with mapDispatchToProps - React Redux

After connecting it all, the flow look like this:

Click to expandClick to expand

How do I test it?

The service

To test our service, we just need to call a custom object as the axios and expect that the get() function is called with the repos url.


<iframe src="" frameborder=0></iframe>

The action

To test our action, we will setup a custom dispatch() and service to simulate the four possibilities: success, empty repos, username not found and connection error.


<iframe src="" frameborder=0></iframe>

The container

Because we separate our connection container from the component, it is easier to test it separately.

<iframe src="" frameborder=0></iframe>

The other components

There are more code files to cover all the project. But those are simple implementation of basic React. Comment it now would be out of context, besides making this article too long. If you want to explore more details, here is the GitHub repository: andregardi/async-redux-without-middlewares *Contribute to andregardi/async-redux-without-middlewares development by creating an account on GitHub.*

Note: I added, to the final project,@material-ui/core and @material-ui/icons for visual components and expressto host static servers on Heroku.

You can’t perform that action at this time.