Ashish prajapati
Posted on October 16, 2024
Deploying a React app on Netlify is a great choice for seamless deployment, as it provides continuous integration with Git and an easy setup. Below is a detailed guide on how to deploy a React app on Netlify, including both manual and Git-based deployment.
Prerequisites:
- React app: You need a working React app. If you don’t have one, you can create it using:
npx create-react-app my-app
GitHub/GitLab/Bitbucket account: If you want to use automatic deployment, ensure your project is in a Git repository.
Netlify account: Sign up at Netlify.
Step 1: Build the React App for Production
Before deploying, you need to create an optimized build of your React app:
- Navigate to your React app folder:
cd my-app
- Run the following command to build the app:
npm run build
This creates a build
folder in your project directory, which contains the optimized files ready for deployment.
Step 2: Deploy the React App on Netlify
There are two ways to deploy your React app on Netlify: Automatic Deployment via Git or Manual Deployment. Choose one of the methods below.
Option 1: Automatic Deployment via Git
In this method, every time you push changes to GitHub, GitLab, or Bitbucket, Netlify automatically redeploys your app.
2.1 Push the React App to GitHub (or GitLab/Bitbucket)
Make sure your React app is pushed to a Git repository. If you haven’t done so:
- Initialize Git in your React project folder:
git init
- Add your files to the repository:
git add .
git commit -m "Initial commit"
- Create a new repository on GitHub and link it:
git remote add origin https://github.com/username/my-app.git
git push -u origin master
2.2 Connect GitHub Repository to Netlify
Login to Netlify: Go to Netlify and log in (or sign up if you haven’t).
Create a new site: Once logged in, click "New Site from Git" on the Netlify dashboard.
Choose your Git provider: Select GitHub, GitLab, or Bitbucket and allow Netlify to access your repositories.
Select the repository: From the list of repositories, choose the one where your React app is located.
-
Configure the build settings:
-
Branch to deploy: Typically, this is
master
ormain
. -
Build command:
npm run build
-
Publish directory:
build
(this is the folder Netlify will deploy from).
-
Branch to deploy: Typically, this is
Deploy the site: Click on "Deploy Site" and Netlify will automatically build and deploy your React app.
2.3 Automatic Redeployment
From now on, whenever you push changes to your GitHub repository, Netlify will automatically rebuild and redeploy your React app.
Option 2: Manual Deployment via Drag-and-Drop
If you prefer to deploy manually, or you don’t want to link your Git repository, you can upload the production build folder directly to Netlify.
2.1 Build the App Locally
Follow the same build step as before:
npm run build
This will create a build
folder containing your production-ready files.
2.2 Deploy via Netlify’s Drag-and-Drop Interface
Login to Netlify: Go to Netlify and log in (or sign up).
Deploy manually: In the Netlify dashboard, click on "Sites", then drag and drop the entire
build
folder into the deployment area on the page.
Netlify will automatically upload the files and deploy your site. You will then be given a URL where your app is hosted.
Step 3: Setting Up a Custom Domain (Optional)
Once your React app is deployed, you might want to add a custom domain instead of using the default Netlify subdomain.
- In the Netlify dashboard, go to Domain Settings for your site.
- Under Custom Domains, click "Add custom domain".
- Follow the instructions to point your custom domain (e.g.,
www.mycustomdomain.com
) to your Netlify site.
If you don’t have a custom domain, you can use Netlify's provided subdomain (like my-app.netlify.app
).
Step 4: Handling Routes in Single Page Applications (React Router)
If you are using React Router in your app, refreshing a page other than the homepage may lead to a 404 error on Netlify. This happens because React Router handles routing on the client side, while Netlify looks for actual files on the server.
Solution: Create a _redirects
File
In your
public
folder, create a new file called_redirects
.Inside the
_redirects
file, add the following line:
/* /index.html 200
This tells Netlify to redirect all requests to index.html
, allowing React Router to handle the routes.
Step 5: Testing and Redeployment
Once your app is live, be sure to:
- Test the app for any issues like broken links or API calls.
- If you need to make updates, you can simply push new commits to your Git repository (if using automatic deployment) or manually upload the new
build
folder (if using manual deployment).
Summary
-
Build the React app using
npm run build
. -
Deploy the app either:
- Automatically via Netlify Git Integration (GitHub, GitLab, or Bitbucket).
- Manually via Netlify’s Drag-and-Drop feature.
-
Fix routing issues with React Router by creating a
_redirects
file. - Optionally, set up a custom domain for your site.
That’s it! Your React app should now be successfully hosted on Netlify and accessible to the world.
Posted on October 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.