</>
Skip to content
React lessons (39/47)

React — Deployment

Build for Production

npm run build

Deploy to Netlify

# Install Netlify CLI
npm install -g netlify-cli

# Deploy
netlify deploy --prod --dir=build

Deploy to Vercel

# Install Vercel CLI
npm install -g vercel

# Deploy
vercel --prod

Deploy to GitHub Pages

# Install gh-pages
npm install --save-dev gh-pages

# Add to package.json
"scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
}

# Deploy
npm run deploy

Environment Variables

# .env
REACT_APP_API_URL=https://api.example.com
REACT_APP_API_KEY=your-api-key

# Access in code
const apiUrl = process.env.REACT_APP_API_URL;

Deployment Examples

# Docker deployment
# Dockerfile
FROM node:14 AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

# Build and run
docker build -t my-react-app .
docker run -p 3000:80 my-react-app

Deployment Checklist

  1. Build for production
  2. Optimize images
  3. Minify CSS and JS
  4. Enable gzip compression
  5. Set up environment variables
  6. Configure routing (if using React Router)
  7. Set up SSL certificate
  8. Configure caching headers

Mini Practice

Write React code that:

  1. Builds for production
  2. Deploys to a hosting service
  3. Uses environment variables
  4. Configures routing for deployment

Up Next

Next: Learn about Context API.

Related Topics

Frequently Asked Questions about Deployment

What is Deployment in React?

Deployment is a fundamental concept in React. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Deployment?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Deployment.

Why is Deployment important in React?

Deployment is essential for React development. Understanding this concept will help you write better code and solve real-world problems more effectively.