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
- Build for production
- Optimize images
- Minify CSS and JS
- Enable gzip compression
- Set up environment variables
- Configure routing (if using React Router)
- Set up SSL certificate
- Configure caching headers
Mini Practice
Write React code that:
- Builds for production
- Deploys to a hosting service
- Uses environment variables
- 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.