Setup React and Redux Development Environment From Scratch

Introduction

This article is intended for beginners who wish to understand the environment setup for React and Redux application and get to know the nitty-gritty of the application building blocks that are getting created using the templates. For example, to create a React application, we can directly execute create-react-app in the terminal with the help of node but we may not understand the basic application completely as we are not aware of all those packages that have been added to our package.json file. Let's demystify the concepts with the help of simple book store application.

Background

Around a year ago, we adopted a new framework based on react and redux library and unfortunately in my team no one was much aware of that. So to learn, people started creating simple applications with ready to use templates but no one knew what packages were included in the simple application and how the application could be modified with different scripts or what a webpack could do to our application. To learn more on React basics, navigate to this page.

Prerequisites

Install NodeJS and Visual Studio Code into your development machine.

Setting the Base Environment

To create reactjs starter application, Facebook has provided a starter template to setup a basic React development environment. So let’s create our book store application by issuing the below command:

Once it is done, we will be able to see the folder structure as below. We can see the files and the dependencies added into package.json and there are few things that are made hidden for us. If you want to see it, you can try npm eject. However, we don't need to see it as anyways, we are going to setup everything ourselves.

Now in the terminal, we can dive into the application directory where our package.json resides and run the application:

And we can see the application running on port 3000. So this is how we setup Reactjs development environment with ready to use starter template.

Alternatively, to setup Reactjs with Redux, we can directly hit npx create-react-app book-store --template redux and it will add Redux dependencies with proper file structure.

Demystifying Generated Application

So now let’s start to setup our environment from scratch. The very first step would be to delete everything from src folder that got generated. Or we can create entirely new folder src and a package.json.

Below are the dependencies that I am going to add to my book store application.

Now let’s dwell into understanding and adding required dependencies.

What is Redux?

So here is the definition of Redux from Redux site Redux is a pattern and library for managing and updating application state, using events called “actions”. When and where it is used can also be found in Redux website here.

I am not going through definitions as the scope and intention of this article is basically to uncover what has been used in generated application and how we can add on our own to understand for better clarity.

Adding Redux

To add Redux into our application, we can hit the below commands:

It is also imperative to understand what all dependencies are used to run the application and which all are needed for the development. To add a dependency, we can issue the following commands:

The flag --save-prod will add your installed package into " dependencies" section and --save-dev will create new section named " devDependencies"

First thing first, let’s add index.html and index.js file:

Let’s add some basic HTML boiler plate code:

Configuring and Understanding webpack

Webpack is bundler to minify js and facilitate adding various plugins and allowing us to add rules as per the file extensions.

Now let’s add webpack and configure in our app.

Create a file with name webpack.config.dev.js for dev environment and start adding below code. This is just naming convention that is preferred to identify webpacks for dev, production or any other environment we need.

So first, we will use commonJS syntax to import webpack, path and “ html-webpack-plugin".

Adding output:

Adding dev server configuration:

Now we will add the HtmlWebpackPlugin that we imported.

Module specifies what all rules we need to add for various file extensions. So let’s add the module now as below:

In the above rules configuration, I have added rule for.js and .jsxwherein all files of that extension within node_modules folder will be excluded and babel-loader will allow transpiling our JS and JSX files using babel. Similarly, we can add style-loader and css-loader for injecting CSS to DOM and importing CSS to our components subsequently. There are other loaders available too like sass-loader less-loader json-loader etc. which we can use as per our requirements. But for now, to keep it simple, I am not going to use other loaders.

Finally, our webpack.config.dev.js should look like this:

Configuring Babel

Babel is a JavaScript compiler which facilitates the following to support all new browsers:

  1. Transpile modern JS(ES5, ES6 and etc.)
  2. Compile JSX to JS

If you want to play around with babel and understand how JSX is tranpiled, you can navigate to https://babeljs.io/ and try it out. 😊

So let’s begin adding babel configuration in package.json and add a preset. So the question now is what are preset??

Here is the answer:

FYI. we can also configure babel in.babelrc file.

Adding Scripts

Now let’s setup scripts to start our book store application.

Like we added start to run the application, we can add multiple scripts to include running test cases or debugging or may be just compiling.

Now, if we try to start the application via npm start, we see an error for linting.

Configuring ESLint

Linting is very important to configure as we can get the errors understandable and easy to fix. So let’s configure eslint into our application. Again, we can setup this in two ways. Either in .eslintrc file or to better have handhold in package.json file.

Once added properly, you should be able to see something like below:

Let’s try running again and after it is successfully compiled, we can navigate to http://localhost:3000.

Finally it’s “ Hello World ".

Summary

So in this article, we saw how actually we can create a React application development environment by knowing what actually a package.json is composed of. And what all dependencies have been added to make the application structure better. Next, we will be creating a simple book store application and configuring Redux with React.

History

Originally published at https://www.codeproject.com on October 28, 2020.

Web developer in C#, Asp.Net, .NetCore. Worked on MVC, Silverlight, WPF, WCF and Windows Phone 8 development. Front end skill --> Angular, React, Backbone