Fix Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. in React
This error means a component keeps re-rendering endlessly because state is being set directly in the render body or an event handler calls setState immediately without user interaction. Fix it by ensuring setState calls are inside event handler callbacks or useEffect hooks, never at the top level of the component body.
Reading the Stack Trace
Here's what each line means:
- at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:16305:18): React detects the render loop here and throws after exceeding the maximum render count.
- at updateFunctionComponent (node_modules/react-dom/cjs/react-dom.development.js:19588:20): The function component is being re-rendered because setState was called during the render phase.
- at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:26466:5): React's synchronous work loop keeps processing the same component because each render triggers another setState.
Common Causes
1. Calling setState directly in render body
Calling setState at the top level of the component causes an immediate re-render, which calls setState again, creating an infinite loop.
export default function Counter() {
const [count, setCount] = useState(0);
setCount(count + 1);
return <p>{count}</p>;
}
2. Calling a function instead of passing a reference
Using onClick={handleClick()} invokes the function immediately during render instead of attaching it as a callback. If handleClick sets state, this creates an infinite loop.
export default function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => setCount(count + 1);
return <button onClick={handleClick()}>Count: {count}</button>;
}
3. useEffect with missing dependency array
Omitting the dependency array from useEffect causes it to run after every render, and if it sets state, it triggers another render indefinitely.
export default function DataLoader() {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data').then(r => r.json()).then(setData);
});
return <pre>{JSON.stringify(data)}</pre>;
}
The Fix
Remove the parentheses after handleClick in the onClick prop. Passing handleClick without parentheses passes the function reference as a callback. With parentheses, the function is called immediately during render, setting state and triggering the infinite loop.
export default function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => setCount(count + 1);
return <button onClick={handleClick()}>Count: {count}</button>;
}
export default function Counter() {
const [count, setCount] = useState(0);
const handleClick = () => setCount(count + 1);
return <button onClick={handleClick}>Count: {count}</button>;
}
Testing the Fix
import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';
describe('Counter', () => {
it('renders without infinite loop', () => {
render(<Counter />);
expect(screen.getByText('Count: 0')).toBeInTheDocument();
});
it('increments count on click', () => {
render(<Counter />);
fireEvent.click(screen.getByRole('button'));
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});
it('increments multiple times', () => {
render(<Counter />);
const button = screen.getByRole('button');
fireEvent.click(button);
fireEvent.click(button);
expect(screen.getByText('Count: 2')).toBeInTheDocument();
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/react-infinite-render-loop,git add src/components/Counter.tsx src/components/__tests__/Counter.test.tsx,git commit -m "fix: pass function reference instead of calling it in onClick",git push origin fix/react-infinite-render-loop
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run the test suite locally to confirm no infinite render loop occurs.
- Open a pull request with the event handler fix.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify stable rendering in staging.
Frequently Asked Questions
BugStack validates the component renders without exceeding React's render limit, runs your test suite, and checks that no downstream components are impacted before marking it safe.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
React will freeze the browser tab and throw this error in the console. React DevTools Profiler can also show components that re-render an unusual number of times.
StrictMode intentionally double-invokes renders in development to surface side effects, but it does not cause this error. If you see this error, there is a real infinite loop.