Introduction

In this article, we are going to explore how to deploy a React app to GitHub pages.

What are GitHub pages?

Types of GitHub Pages

User Site

Organization Site

How to Publish the React application on GitHub Pages

To publish the user site, follow the below steps.

  1. Create an Account in GitHub.
  2. Create Repo in GitHub where the repo name should be username.github.io
  3. Create React Application.
  4. Deploy the React Application using GitHub Pages.
  5. Commit and Push the codebase (React Application) into GitHub repo.

Step 1 - Create an Account in GitHub

Step 2 - Create Repo in GitHub

Step 3 - Create React Application

Create the react application which you want to host as a website. In this article, I am mainly focusing on how to deploy the react application on GitHub Pages. So please refer to this link to create React application.

Step 4 - Deploy the React Application

To deploy the application, follow the below steps.

1) Add GitHub Pages dependency package

Install "gh-pages" package using the below command.

npm install gh-pages — save-dev

2) Add homepage property to package.json file

3) Add deploy scripts to package.json file

Add both predeploy and deploy property scripts to the package.json file as below,

"predeploy": "npm run build",
"deploy": "gh-pages -d build"

The "predeploy" command is used to bundle the react application and the "deploy" command helps to deploy the bundled file.

4) Create a remote GitHub repository

5) Deploy the Application to GitHub Pages

Now run the below command to deploy your react application to GitHub Pages.

npm run deploy

6) Access deployed site

To get the published URL,

Now, you can access the deployed site using the published URL!

Step 5 - Commit and Push the codebase

Commit and push your code changes to the GitHub repo. It is an optional one. But, for maintaining the code base we can do this step.

git add .
git commit -m "commit message"
git push origin master