DEV Community

Cover image for How to Deploy React to Netlify
Raynaldo Sutisna
Raynaldo Sutisna

Posted on

4 1

How to Deploy React to Netlify

Step One: Login to Netlify

Click New site from Git on the home page after you are login.
first-step

Step Two: Choose your git provider

Choose what git provider that you use. I am choosing Github in this step.
second-step

Step Three: Choose your repository

Search and choose repository you want to deploy
third-step

Step Four: Build options and deploy

  • You can choose any branch from your repository instead of the main branch.
  • Type this CI= npm run build in the Build Command.
  • Click Deploy site! fourh-step

Step Five: Change the site name (Optional)

  • Go to Site Settings. site-name-first
  • Click Site details and Change site name. site-name-second
  • Change the name and save. site-name-third

React Router DOM Problem

Page Not Found
Looks like you're followed a broken link or entered a URL that doesn't exist on this site.
Enter fullscreen mode Exit fullscreen mode

If you are using react-router-dom and you get this error message when you try reload the route page.
error

Create a _redirects file inside the public folder and copy this code

/* /index.html 200
Enter fullscreen mode Exit fullscreen mode

_redirects

Bonus: Set fetch baseURL dynamically (on Development or on Production)

Changing API baseURL whenever I deploy to the server is really painful because I always forget to do that. However, I found a trick to handle this problem.

I am using axios for handling fetch-request and I set up this in the index.js

// ./src/index.js
...

if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
  // development URL
  axios.defaults.baseURL = `${process.env.REACT_APP_API_SERVER_DEVELOPMENT}/api/v1`;
} else {
  // production URL
  axios.defaults.baseURL = `${process.env.REACT_APP_API_SERVER_PRODUCTION}/api/v1`;
}

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById("root")
Enter fullscreen mode Exit fullscreen mode

What is process.env.REACT_APP_API_SERVER_DEVELOPMENT and process.env.REACT_APP_API_SERVER_PRODUCTION? I create .env to save the baseURL.

./src/.env
REACT_APP_API_SERVER_DEVELOPMENT=http://localhost:3001
REACT_APP_API_SERVER_PRODUCTION=https://herokuy-deploy-test.herokuapp.com
Enter fullscreen mode Exit fullscreen mode

We are good to forget changing URL for production.

https://netlikuy-deploy-test.netlify.app/

GitHub logo raaynaldo / netlikuy-deploy-test

learn how to deploy React App to Netlify

Deploy your Rails API?

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
dance2die profile image
Sung M. Kim

Thanks for sharing, Raynaldo~

I see a lot of these deploy-to-Netlify posts but as Netlify changes (not breaking mostly) overtime, this is helpful to see a working post ~

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay