Skip to content

Akhila Ariyachandra

Getting started in React with Parcel.js

React, JavaScript, Parcel.js2 min read

Updated on 27.11.2019 with routing added.

tl;dr - Clone and run the source code.

Usually when starting to work on a React project, developers go with create-react-app. While this is a great option for many cases, I find it to be a bit too bloated, especially after ejecting it. I also find that it takes a little of work to manually setup and maintain a webpack config. Parcel is great for use with React as there is nothing to configure while setting up. It also helps that building the app in Parcel is also really fast.

First lets initialize a project with either yarn or npm. I'll be using yarn.

Then let's install Parcel as a dev dependency.

After that let's setup babel by installing the dev dependencies and creating the .babelrc file

Once the dependencies are done installing create the .babelrc file in the project root with the following code.

This is all the setup we will need to for Parcel to work with React. Now let's setup React. First we will need to the React dependencies.

If you want to use async/await in your code, an additional dependency is needed.

Next we will need an entry point for out app. The nice thing about Parcel is that the entry file doesn't have to be a JavaScript file. In our case its going to be a HTML file. Create a folder with the name src. This folder is going to contain all the source code. In the src folder create the index.html file which is going to be the entry point of the app and add the following code.

After that we will create the index.js file (also in src) which will connect the root React component to the index.html file.

Next let's create the root component in the App.js file. The root component is going to contain the routing configuration.

After that let's create a layout for our pages. Create the folder components in src and in it create Layout.js. The layout component is also going to contain the navigation links within the app.

Then let's create the pages. In src create a folder called pages. The first page we are going to create is the home page.

After that we will create the about page.

Finally all that's left to do is to add the scripts to run the app. Add the following in the package.json file.

dev will be used to run the development of the app. Don't worry about restarting the server after making changes to the code while it's running, as Parcel will automatically take care of that. build is used to make the production version of the app in the dist folder in the project root. Let's check if everything has been setup properly by running the dev command.

When you visit localhost:1234 in your browser you should be seeing

Home Page

When you navigate to "About" you should see

About Page

Now you can continue creating your React app as usual from here. The source code for everything done here is available in GitHub.