How to Deploy a React Router-Based Application to Netlify (2024)

In this article, we'll learn the most popular ways of deploying a React app to Netlify. We'll also learn the configuration changes you'll need to make to deploy a Routing-based React app.

The thing I love about Netlify is that it provides a lot of useful features
for free such as:

  • A way to deploy a static website within seconds
  • Continuous deployment, which means when you connect your Github/Gitlab/Bitbucket repository, it automatically triggers deployment when new commits are pushed to the repository
  • Assurance that your website never goes down, even during new deployments
  • Allows you to easily rollback to any previous working version of your site with a single click
  • Lets you quickly preview any of the previously deployed versions of the app
  • Change the domain or subdomain of your site instantly

and much more.

So let's see how to deploy a React app to Netlify.

Want to learn Redux from the absolute beginning and build a food ordering app from scratch? Check out the Mastering Redux course.

Drag and Drop the Build Folder in Netlify

The fastest and easy way to deploy a React application is just to drag and drop the build folder in Netlify.

To create a build folder, just execute the npm run build or yarn build command from the command line from your project folder.

Once the build folder is created, you just need to drop the folder in the drop area under the sites menu, as shown below:

How to Deploy a React Router-Based Application to Netlify (1)

How to Deploy an App to Netlify from a GitHub Repository

This is my favorite way of deploying applications on Netlify.

Because whenever you push any changes to the GitHub repository, it will automatically be deployed to Netlify. You can also see all deployed versions and easily roll back to any previously working version of code with just a single click.

If you already have a repository pushed to GitHub, then you just need to connect it.

Login to your Netlify account. In the dashboard, click on the New site from Git button.

How to Deploy a React Router-Based Application to Netlify (2)

Click on the GitHub button to connect your GitHub repository.

How to Deploy a React Router-Based Application to Netlify (3)

It will open a new tab. Make sure the popup is enabled in your browser.

How to Deploy a React Router-Based Application to Netlify (4)

Search for the GitHub repository in the Search repos search box. If your repository is not getting displayed then click on the Configure the Netlify app on GitHub button at the bottom of the page.

How to Deploy a React Router-Based Application to Netlify (5)

Once clicked, scroll down on the page and click on the Select repositories dropdown and search for your repository and click on the Save button.

How to Deploy a React Router-Based Application to Netlify (6)

You will be redirected to the previous page showing all the available repositories.

Search for the repository you want to deploy. For this article, I have selected the react-book-management-app repository which we created in my previous article.

How to Deploy a React Router-Based Application to Netlify (7)

Once you select the repository, you will see the following screen:

How to Deploy a React Router-Based Application to Netlify (8)

For this application, we don't need to change anything.

Your Build command and Publish directory will be automatically populated. Make sure to enter these fields if you have a different command in package.json to build your app or those fields are not auto-populated.

Now, click on the Deploy site button. Once clicked, you will see the Site deploy in progress message.

How to Deploy a React Router-Based Application to Netlify (9)

You'll have to wait a little bit while it's deploying. Once deployment is completed, you will see the following screen:

How to Deploy a React Router-Based Application to Netlify (10)

Open the link in the new tab and you will see your application deployed live.

How to Deploy a React Router-Based Application to Netlify (11)

Awesome! Now, if you make any changes in the source code and push that change to GitHub, Netlify will detect that change and re-deploy your application with your latest changes.

If you check the application, you will see that the application works just fine with the navigation and you're able to add/edit/delete a book.

How to Deploy a React Router-Based Application to Netlify (12)

But there is one issue. If you directly access the /add route or refresh the /add route page, you will get a page not found error as shown below:

How to Deploy a React Router-Based Application to Netlify (13)

You will get the same error if you try to refresh the edit page route.

This is because when we access any route on our local machine, React Router handles the routing. But when we deploy the application on any server, directly accessing the route will send the request to the server itself (Netlify in our case).

But as there is no /add route handler on the server-side, you will see a page not found error. But Netlify provides a way to fix this.

Create a new file with the name _redirects inside the public folder of our project and add the following contents inside it:

/* /index.html 200

Here, we're telling Netlify to redirect all the routes to the index.html file.

The index.html file contains our entire React app code. It gets generated inside the build folder when the yarn build command is executed by Netlify while deploying the app.

And as routing is handled by our React app which is contained in the index.html file, our application will work without a page not found issue.

Now, push the changes to the GitHub repository so Netlify will deploy the app again with these changes.

And once deployed, if you check the deployed application, you will see that the application works fine and we don't get a page not found error.

How to Deploy a React Router-Based Application to Netlify (14)

That's it! We're all done with deploying our application to Netlify.

How to Easily Change a Site Name in Netlify

If you check the name of the deployed site you will see that it's not easy to remember, especially if you have lot of applications deployed. But Netlify provides a way to easily change that.

Click on the Site settings button displayed on the Site overview section.

How to Deploy a React Router-Based Application to Netlify (15)

Then click on the Change site name button and enter a new name. Click on the Save button, and now you can access your application with the changed name.

How to Deploy a React Router-Based Application to Netlify (16)
I usually like to give the same name as the repository name so it's easy to find a particular application if you have a lot of deployed applications on Netlify.

If you want to know how to deploy React + Node.js application to production, check out this article.

Thanks for reading!

You can find the complete GitHub source code along with this redirection change in this repository.

You can see the live demo of the deployed application here.

Want to learn all ES6+ features in detail including let and const, promises, various promise methods, array and object destructuring, arrow functions, async/await, import and export and a whole lot more from scratch?

Check out my Mastering Modern JavaScript book. This book covers all the pre-requisites for learning React and helps you to become better at JavaScript and React.

Check out free preview contents of the book here.

Also, you can check out my free Introduction to React Router course to learn React Router from scratch.

Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.

How to Deploy a React Router-Based Application to Netlify (17)
How to Deploy a React Router-Based Application to Netlify (2024)

FAQs

How do you deploy a React router based application to Netlify? ›

Deploy the React app to Netlify

Linking your GitHub repo to Netlify is also quite straightforward. You simply need to set the build command — usually npm run build for Vite projects—and choose the publish directory as dist/ . This tells Netlify how to build your project and where to get the files to publish.

How to deploy React app with router? ›

Steps:
  1. Step 1: Install React Router. To install react router use the following command. ...
  2. Step 2: Creating Navbar Component. Create Navbar. ...
  3. Step 3: Creating templates. To add routing, we need multiple pages. ...
  4. Step 4: Modify App. js. ...
  5. Step 5: Deployment.
Jul 19, 2023

How to fix page not found in Netlify in React? ›

Make sure you've got the CLI installed and use that to initialize the Netlify config. Once you've done that, see if you can build/deploy without the CI= bit in your build command. It shouldn't be needed once the config is initialized properly. Cheers!

How to deploy a React app with Netlify and set up continuous deployment via GitHub? ›

Steps to Deploy a React App on Netlify using GitHub
  1. Step 1: Create a React Application. At the very first, you need a react application. ...
  2. Step 2: Install the Required Dependencies. ...
  3. Step 3: Create a Build directory. ...
  4. Step 4: Create a GitHub repository. ...
  5. Step 5: Setting up Netlify. ...
  6. Step 6: Other ways to deploy.
May 19, 2023

How to deploy React app on Netlify without GitHub? ›

Build your React app by running the command npm run build or yarn build , this will generate a build folder with the optimized version of your app. Run the command netlify deploy to deploy your app. This command will automatically detect the build folder and upload its files to Netlify.

How do I deploy something on Netlify? ›

Deploy a project to Netlify
  1. Select the Deploy to Netlify button. You will get directed to the Netlify app to create a new site. ...
  2. Select Connect to GitHub to authenticate. ...
  3. Select Save & Deploy. ...
  4. When the site deploy succeeds, you should get a production URL where you can access the website.
Jul 18, 2024

How do you implement react router in react? ›

Here are the basic steps for implementing routing in a React application using React Router:
  1. Install React Router: npm install react-router-dom.
  2. Wrap your application with the BrowserRouter component: ...
  3. Define your routes using the Route component: ...
  4. Use the Link component to navigate between routes:
Mar 29, 2023

How to deploy React app locally? ›

Dive Into React Development: How To Run React App Locally
  1. Prerequisites: Install Node and a Package Manager. ...
  2. Setting Up Your Local Development Environment. ...
  3. Creating a New React App with NPX. ...
  4. Exploring the React App Structure and Important Files. ...
  5. Running the React App Locally with npm start.
Feb 21, 2024

How to deploy react router app on GitHub? ›

To deploy your React app to GitHub Pages, you must push this directory to a particular branch called gh-pages. With these scripts in place, every time you run npm run deploy, the predeploy script will trigger the build process, and the deploy script will push the build to the gh-pages branch of your GitHub repository.

Why is my Netlify build failing? ›

The issue is that your Base directory is set to web and that does not exist when Netlify tries to access it. You could confirm this by looking at your repository and confirming there is a web directory at the root.

How do I handle page NotFound in React router? ›

To effectively manage 404 errors in a react app, you can implement a redirect to a custom 404 page using react router dom. This ensures that when a user navigates to a route that does not exist, they are presented with a user-friendly error page rather than a generic browser error message.

Why is my form not showing up in Netlify? ›

Form detection disabled

If you're not finding new form submissions in your Verified submissions list for updated or newly added forms, ensure that form detection is enabled for your site. to review the setting. Once you enable or re-enable form detection, make sure you redeploy your site.

How do I deploy frontend and backend to Netlify? ›

After your site is created in Netlify, in your Netlify dashboard, navigate to the site's configuration area and:
  1. In the Build & Deploy section, in the Build settings, click Configure.
  2. Find the Runtime setting and select Next. js.
  3. Redeploy the site.

Does Netlify deploy automatically? ›

After you set up Git provider permissions and link a repository, Netlify will run your build command and deploy the result whenever you push to your Git repo. The benefits of Netlify's continuous deployment include: No deploying without committing and pushing first.

How do I deploy React app to domain? ›

  1. Serving the Same Build from Different Paths​
  2. Step 1: Add homepage to package.json ​
  3. Step 2: Install gh-pages and add deploy to scripts in package.json ​
  4. Step 3: Deploy the site by running npm run deploy ​
  5. Step 4: For a project page, ensure your project's settings use gh-pages ​
  6. Step 5: Optionally, configure the domain​
Dec 1, 2022

How do I deploy React router app on GitHub? ›

To deploy your React app to GitHub Pages, you must push this directory to a particular branch called gh-pages. With these scripts in place, every time you run npm run deploy, the predeploy script will trigger the build process, and the deploy script will push the build to the gh-pages branch of your GitHub repository.

How to deploy React app in Netlify FreeCodeCamp? ›

How to deploy a React application to Netlify
  1. Left(Login Page) Center(Authorization) Right(Netlify Online App) ...
  2. Drag & Drop build folder to Netlify Online Application to Deploy. ...
  3. Pop-Up window asking you to log in with Netlify and grant access to the Netlify CLI. ...
  4. It gives us the option to give our site a name.
Jan 31, 2019

How do I get the route URL of a React router? ›

In React Router, use the useLocation() hook to get the current route. It returns an object containing properties like pathname, search, and hash. To get the full URL in a React app, use window. location.

References

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6311

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.