Fixing the “Maximum Update Depth Exceeded” Error in React

How to debug and resolve infinite re-render loops in React components


The Problem: When React Components Go Haywire 🔥

Picture this: You’re working on a React application, everything seems to be working fine, and then suddenly your browser console explodes with this dreaded error:

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.

This error indicates that your component is stuck in an infinite re-render loop, making your application unusable and creating a frustrating user experience.

Understanding the Root Cause 🕵️‍♂️

This error commonly occurs in components that manage dynamic lists or collections, such as sidebar navigation, data tables, or filtered content. Let’s examine a typical scenario where this happens:

The Problematic Code

// ❌ BEFORE: The problematic implementation
const filteredItems = items?.filter(/* filtering logic */) || [];
const groupedItems = filteredItems.reduce(/* grouping logic */, {});
const sortedGroups = Object.entries(groupedItems).sort(/* sorting logic */);

useEffect(() => {
  if (sortedGroups.length > 0) {
    const allGroupNames = sortedGroups.map(([groupName]) => groupName);
    setExpandedGroups(new Set(allGroupNames));
  }
}, [sortedGroups]); // 🚨 This dependency changes on every render!

Why This Caused Infinite Re-renders

  1. Unstable Dependencies: The sortedGroups array was being recreated on every render because the filtering, grouping, and sorting operations weren’t memoized.
  2. State Update Trigger: Every time sortedGroups changed (which was every render), the useEffect would fire and call setExpandedGroups.
  3. Re-render Cascade: The state update would trigger another re-render, which would recreate sortedGroups, which would trigger the useEffect again, and so on…
  4. React’s Safety Net: After detecting this pattern, React threw the “Maximum update depth exceeded” error to prevent an infinite loop from crashing the browser.

The Solution: Memoization and Smart State Updates 🛠️

Here’s a multi-pronged approach to fix this issue:

1. Memoize Expensive Computations

The first step is to wrap expensive operations in useMemo to ensure they only recalculate when their actual dependencies change:

// ✅ AFTER: Memoized filtering
const filteredItems = useMemo(
  () =>
    items?.filter(
      (item: any) =>
        (item.title || '').toLowerCase().includes(searchQuery.toLowerCase()) ||
        (item.description || '')
          .toLowerCase()
          .includes(searchQuery.toLowerCase()) ||
        (item.category?.name || '')
          .toLowerCase()
          .includes(searchQuery.toLowerCase())
    ) || [],
  [items, searchQuery] // Only recalculate when items or searchQuery change
);

// ✅ AFTER: Memoized grouping
const groupedItems = useMemo(() => {
  return filteredItems.reduce((acc: any, item: any) => {
    const groupName = item.category?.name || 'Uncategorized';
    if (!acc[groupName]) {
      acc[groupName] = {
        category: item.category,
        items: [],
      };
    }
    acc[groupName].items.push(item);
    return acc;
  }, {} as Record<string, { category: any; items: any[] }>);
}, [filteredItems]);

// ✅ AFTER: Memoized sorting
const sortedGroups = useMemo(() => {
  return Object.entries(groupedItems).sort(([a], [b]) => {
    if (a === 'Uncategorized') return 1;
    if (b === 'Uncategorized') return -1;
    return a.localeCompare(b);
  }) as [string, { category: any; items: any[] }][];
}, [groupedItems]);

2. Create Stable Dependencies

Extract the group names into a separate memoized value to create a stable dependency for useEffect:

// ✅ AFTER: Stable dependency for useEffect
const groupNames = useMemo(() => {
  return sortedGroups.map(([groupName]) => groupName);
}, [sortedGroups]);

3. Implement Smart State Updates

The most crucial fix is implementing a comparison check before updating the state:

// ✅ AFTER: Smart state updates with comparison
useEffect(() => {
  if (groupNames.length > 0) {
    setExpandedGroups(prev => {
      // Only update if the group names have actually changed
      const currentNames = Array.from(prev).sort();
      const newNames = [...groupNames].sort();

      if (JSON.stringify(currentNames) !== JSON.stringify(newNames)) {
        return new Set(groupNames);
      }
      return prev; // 🎯 Return previous state if no change needed
    });
  }
}, [groupNames]);

The Key Insights 💡

1. Always Return Previous State When No Change is Needed

The most important lesson here is that when using functional state updates, always return the previous state if no actual change is needed. This prevents unnecessary re-renders.

2. Memoize Expensive Operations

Operations like filtering, sorting, and reducing large arrays should be wrapped in useMemo to prevent unnecessary recalculations.

3. Be Careful with Object and Array Dependencies

Objects and arrays are compared by reference in React. Even if their contents are the same, if they’re recreated on each render, React considers them different.

4. Use Functional State Updates for Comparisons

When you need to compare the current state with new values before updating, use the functional form of setState:

setState(prev => {
  // Compare prev with new value
  if (shouldUpdate) {
    return newValue;
  }
  return prev; // Important: return previous state
});

Performance Benefits 📈

After implementing these fixes, you can expect:

  • Eliminated infinite re-renders: Components only re-render when necessary
  • Improved performance: Memoization reduces unnecessary computations
  • Better user experience: UI interactions become smooth and responsive
  • Reduced CPU usage: No more constant re-rendering cycles

Testing the Fix 🧪

To verify the fix works properly:

  1. Monitor React DevTools: Check that components aren’t re-rendering unnecessarily
  2. Add console logs: Temporarily log when expensive operations run
  3. Test edge cases: Ensure the fix works with empty data, single items, etc.
  4. Performance profiling: Use React DevTools Profiler to measure render times

Best Practices to Prevent This Issue 🛡️

  1. Use useMemo for expensive computations that depend on props or state
  2. Use useCallback for event handlers that are passed to child components
  3. Always check if state actually needs to change before calling setState
  4. Be mindful of dependencies in useEffect – ensure they’re stable
  5. Use React DevTools to identify unnecessary re-renders during development

Conclusion

The “Maximum update depth exceeded” error might seem intimidating, but it’s actually React trying to protect your application from infinite loops. By understanding the root cause – usually unstable dependencies or unnecessary state updates – you can implement targeted fixes that not only resolve the error but also improve your application’s performance.

The key takeaways from this fix:

  • Memoize expensive operations with useMemo
  • Create stable dependencies for useEffect
  • Compare before updating state and return previous state when no change is needed
  • Always profile and test your fixes to ensure they work as expected

Remember: React’s error messages are your friend. They’re designed to help you write better, more performant code. When you encounter them, take the time to understand the root cause rather than just patching the symptoms.


Have you encountered similar infinite re-render issues in your React applications? Share your experiences and solutions in the comments below!

Example Implementation

Here’s a complete example of how to implement this fix in your own components:

import { useMemo, useEffect, useState } from 'react';

function ListComponent({ items, searchQuery }) {
  const [expandedGroups, setExpandedGroups] = useState(new Set());

  // Memoized filtering
  const filteredItems = useMemo(
    () =>
      items?.filter(item =>
        item.title.toLowerCase().includes(searchQuery.toLowerCase())
      ) || [],
    [items, searchQuery]
  );

  // Memoized grouping
  const groupedItems = useMemo(() => {
    return filteredItems.reduce((acc, item) => {
      const group = item.category || 'Other';
      if (!acc[group]) acc[group] = [];
      acc[group].push(item);
      return acc;
    }, {});
  }, [filteredItems]);

  // Stable dependency
  const groupNames = useMemo(() => Object.keys(groupedItems), [groupedItems]);

  // Smart state updates
  useEffect(() => {
    if (groupNames.length > 0) {
      setExpandedGroups(prev => {
        const current = Array.from(prev).sort();
        const newNames = [...groupNames].sort();

        if (JSON.stringify(current) !== JSON.stringify(newNames)) {
          return new Set(groupNames);
        }
        return prev;
      });
    }
  }, [groupNames]);

  // Rest of component...
}

Happy debugging!

Mocking in Next.js with Jest: How to create mocks for API responses and dependencies

Mocking is an essential part of unit testing in Next.js with Jest. It allows us to create a fake version of a dependency or API response and test our code in isolation. In this blog post, we will explore how to create mocks for API responses and dependencies in Next.js with Jest.

What is mocking?

Mocking is the process of creating a fake version of a dependency or API response that our code depends on. By creating a mock, we can test our code in isolation without relying on external dependencies. This allows us to control the behavior of the mocked dependency or API response and test various scenarios.

Why use mocking?

There are several benefits to using mocking in our tests:

  • Isolation: By mocking dependencies and API responses, we can test our code in isolation without relying on external factors.
  • Control: We can control the behavior of the mocked dependency or API response and test various scenarios.
  • Speed: Mocking can make our tests run faster by reducing the need for external calls.

Creating mocks for API responses

When testing Next.js applications that rely on external APIs, we can create mocks for API responses using Jest’s jest.mock() function. This function allows us to replace the original module with a mock module that returns the data we want.

Here’s an example of how to create a mock for an API response in a Next.js application:

// api.js
import axios from 'axios';

export async function getUsers() {
  const response = await axios.get('/api/users');
  return response.data;
}

// __mocks__/axios.js
const mockAxios = jest.genMockFromModule('axios');

mockAxios.get = jest.fn(() => Promise.resolve({ data: [{ id: 1, name: 'John' }] }));

export default mockAxios;

In this example, we have created a mock for the **axios**module that returns a fake response with a single user. The mock is defined in the **__mocks__**directory, which is automatically recognized by Jest.

To use this mock in our test, we can simply call **jest.mock('axios')**at the beginning of our test file:

// api.test.js
import { getUsers } from './api';
import axios from 'axios';

jest.mock('axios');

describe('getUsers', () => {
  it('returns a list of users', async () => {
    axios.get.mockResolvedValue({ data: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }] });

    const result = await getUsers();

    expect(result).toEqual([{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]);
  });
});

In this test, we have mocked the axios.get() method to return a list of two users. We then call the getUsers() function and assert that it returns the correct data.

Creating mocks for dependencies

In addition to mocking API responses, we can also create mocks for dependencies that our code depends on. This can be useful when testing functions that rely on complex or external dependencies.

Here’s an example of how to create a mock for a dependency in a Next.js application:

// utils.js
import moment from 'moment';

export function formatDate(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
}

// __mocks__/moment.js
const moment = jest.fn((timestamp) => ({
  format: () => `Mocked date: ${timestamp}`,
}));

export default moment;

In this example, we have created a mock for the moment module that returns a formatted string with the timestamp value. The mock is defined in the __mocks__ directory, which is automatically recognized by Jest.

To use this mock in our test, we can simply call jest.mock('moment') at the beginning of our test file:

// utils.test.js
import { formatDate } from './utils';
import moment from 'moment';

jest.mock('moment');

describe('formatDate', () => {
  it('returns a formatted date string', () => {
    const timestamp = 1617018563137;
    const expected = 'Mocked date: 1617018563137';

    const result = formatDate(timestamp);

    expect(moment).toHaveBeenCalledWith(timestamp);
    expect(result).toEqual(expected);
  });
});

In this test, we have mocked the moment() function to return a formatted string with the timestamp value. We then call the formatDate() function and assert that it returns the correct string.

Conclusion

Mocking is an essential part of unit testing in Next.js with Jest. It allows us to create a fake version of a dependency or API response and test our code in isolation. In this blog post, we explored how to create mocks for API responses and dependencies in Next.js with Jest. We saw how to use jest.mock() to create mocks for external APIs and how to create mocks for dependencies. By using mocking in our tests, we can test our code in isolation, control the behavior of dependencies and API responses, and make our tests run faster.