React10 min read

Fixing CORS Errors in React Applications

Learn how to handle and fix CORS errors in React applications. Understand proxy configuration, fetch options, and how to work with APIs. Essential for React developers.

David Park
December 19, 2025
0.0k0

CORS errors in React happen when your app tries to call an API from a different origin. Understanding how to handle this is crucial for building React applications.

What Causes CORS Errors in React?

When your React app (running on localhost:3000) tries to fetch data from an API (running on localhost:5000), the browser blocks it due to CORS policy. This is a browser security feature.

Solutions for Development

For development, you have several options: - Use a proxy in package.json - Use a proxy in vite.config.js (if using Vite) - Configure the backend to allow your origin

Solutions for Production

In production, you need to: - Ensure backend allows your frontend domain - Use proper CORS configuration - Handle credentials correctly

Using Fetch with CORS

Learn how to properly configure fetch requests to work with CORS-enabled APIs. This includes setting headers and credentials.

Proxy Configuration

Setting up a proxy is the easiest solution for development. It makes your API calls appear to come from the same origin.

Common Mistakes

I'll show you common mistakes developers make and how to avoid them. These tips will save you hours of debugging.

#React#CORS#API#Fetch#Frontend

Common Questions & Answers

Q1

How do I fix CORS errors in React development?

A

Use proxy in package.json: add "proxy": "http://localhost:5000". Or use fetch with mode: "cors" and ensure backend allows your origin. For Vite, configure proxy in vite.config.js. Best solution is configuring backend CORS properly.

javascript
// package.json (Create React App)
{
  "name": "my-app",
  "version": "0.1.0",
  "proxy": "http://localhost:5000"
}

// Then in React, use relative URLs
fetch('/api/data')
  .then(res => res.json())
  .then(data => console.log(data));

// Or with full URL and CORS
fetch('http://localhost:5000/api/data', {
  method: 'GET',
  mode: 'cors',
  credentials: 'include',
  headers: {
    'Content-Type': 'application/json'
  }
})
.then(res => res.json())
.then(data => console.log(data));
Q2

How do I configure proxy in Vite for React?

A

In vite.config.js, add server.proxy configuration. This forwards API requests to your backend during development. Different from Create React App proxy but works similarly.

javascript
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:5000',
        changeOrigin: true,
        secure: false
      }
    }
  }
});

// Then use in React
fetch('/api/data')
  .then(res => res.json())
  .then(data => console.log(data));
Q3

How do I handle CORS with credentials in React?

A

Set credentials: "include" in fetch options. Backend must allow your origin specifically (not *) and set Access-Control-Allow-Credentials: true. Both frontend and backend must be configured correctly.

javascript
// React component
function MyComponent() {
  useEffect(() => {
    fetch('http://localhost:5000/api/user', {
      method: 'GET',
      credentials: 'include',  // Send cookies
      headers: {
        'Content-Type': 'application/json'
      }
    })
    .then(res => res.json())
    .then(data => setUser(data))
    .catch(err => console.error('CORS error:', err));
  }, []);

  return <div>User data here</div>;
}

// Backend must allow credentials
app.use(cors({
  origin: 'http://localhost:3000',
  credentials: true
}));