Skip to main content

A Step-by-Step Guide to Using GitHub Pages for Your React App

One excellent option for hosting your React app is GitHub Pages. It allows you to deploy your application directly from your GitHub repository, making it accessible to users worldwide. In this blog post, we will walk you through the process of using GitHub Pages to host your React app. So, let's get started!

Step 1: Create a React App

Assuming you have Node.js and npm installed on your machine, begin by creating a new React application using Create React App. Open your terminal and run the following command:

npx create-react-app my-app

This command creates a new directory named "my-app" with the basic structure of a React application.

Step 2: Set Up a GitHub Repository

Navigate to GitHub (github.com) and create a new repository. Give it a name, add an optional description, and choose any additional settings you prefer. Ensure that the repository is public so that it can be accessed through GitHub Pages.

Step 3: Connect Your Local React App to the GitHub Repository

Once your repository is set up, connect your local React app to the GitHub repository by initializing it as a Git repository. In your terminal, navigate to the project's root directory and run the following commands:

git init
git remote add origin <repository-url>
  

Replace <repository-url> with the URL of your GitHub repository.

Step 4: Configure Deployment Settings

Next, you need to configure the deployment settings for your React app. Open the package.json file in the root of your project directory and add the following lines:

"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"homepage": "http://<your-username>.github.io/<repository-name>"
  

Make sure to replace <your-username> with your GitHub username and <repository-name> with the name of your GitHub repository.

Step 5: Install and Configure gh-pages

Install the gh-pages package by running the following command in your terminal:

npm install gh-pages --save-dev

Once the installation is complete, you need to configure the gh-pages package. Open the package.json file again and add the following line at the top-level:

"homepage": "http://<your-username>.github.io/<repository-name>"

Step 6: Deploy Your React App

To deploy your React app to GitHub Pages, run the following command in your terminal:

npm run deploy

This command builds your React app and deploys it to the gh-pages branch of your GitHub repository.

Step 7: Access Your Deployed React App

Finally, to access your React app deployed on GitHub Pages, open a web browser and enter the URL in the following format:

https://<your-username>.github.io/<repository-name>

Congratulations! You have successfully deployed your React app using GitHub Pages.

GitHub Pages provides a straightforward and efficient way to host your React applications and share them with others. By following the steps outlined in this guide, you can quickly deploy your React app and make it accessible to a global audience. So, why wait? Start showcasing your amazing React projects using GitHub Pages today!

Sample Repository: https://github.com/sudeepcv/react-github-page-demo

Deployed Page: https://sudeepcv.github.io/react-github-page-demo/

Comments