Getting started in React with Parcel.js

Getting started in React with Parcel.js

Posted on 15th June 2019

Last updated on 27th November 2019

5 min read

Updated on 27th November, 2019 with routing added.

If you want a version with Webpack instead of Parcel, please check out my post here.

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.

shell

Then let’s install Parcel as a dev dependency.

shell

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

shell

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

.babelrc

json

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.

shell

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

shell

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.

src/index.html

html

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

src/index.js

jsx

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

src/App.js

jsx

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.

src/components/Layout.js

jsx

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.

src/pages/home.js

jsx

After that we will create the about page.

src/pages/about.js

jsx

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

package.json

json

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.

shell

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.

0 views

Leave a Reaction below!!! (No login required)