Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 13, 2025

Problem

Users encountered a "Maximum update depth exceeded" error when learningmap-data was present in localStorage, causing the application to crash with an infinite loop:

Uncaught Error: Maximum update depth exceeded. This can happen when a component 
repeatedly calls setState inside componentWillUpdate or componentDidUpdate. 
React limits the number of nested updates to prevent infinite loops.

Root Cause

The useEffect hook in LearningMap.tsx (lines 115-127) included getViewport and getRoadmapState in its dependency array:

useEffect(() => {
  const viewport = getViewport();
  const minimalState = getRoadmapState(viewport);
  
  if (onChange) {
    onChange(minimalState);
  }
  // ...
}, [nodes, onChange, getViewport, getRoadmapState]); // ❌ Problem

While getViewport (from React Flow's useReactFlow() hook) and getRoadmapState (from Zustand store) are stable functions, including them in the dependency array caused React to detect changes and re-trigger the effect under certain conditions. This created an infinite loop:

  1. Effect runs when nodes change
  2. Calls onChange callback
  3. Parent component may re-render
  4. React detects function reference change (even though functions are stable)
  5. Effect triggers again
  6. Infinite loop → crash

Solution

Removed the unnecessary function dependencies from the dependency array:

}, [nodes, onChange]); // ✅ Only track actual state values

This follows React best practices for stable function references from hooks and stores. Functions from Zustand stores and React Flow hooks don't change between renders and don't need to be tracked as dependencies.

Testing

  • ✅ TypeScript type checking passes
  • ✅ All packages build successfully
  • ✅ CodeQL security scan passes (0 alerts)
  • ✅ No breaking changes to API or behavior

Changes

  • Modified packages/learningmap/src/LearningMap.tsx - removed 2 function dependencies from useEffect dependency array

This is a minimal, surgical fix that resolves the infinite loop while maintaining all existing functionality.

Original prompt

I get the following error, when learningmap-data is present in localStorage:

react-dom-client.development.js:4624 Uncaught Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
at getRootForUpdatedFiber (react-dom-client.development.js:4624:11)
at enqueueConcurrentRenderForLane (react-dom-client.development.js:4588:14)
at forceStoreRerender (react-dom-client.development.js:8261:18)
at react-dom-client.development.js:8247:11
at vanilla.mjs:9:39
at Set.forEach ()
at setState (vanilla.mjs:9:17)
at setNodes (index.js:3252:17)
at index.js:294:17
at Object.react_stack_bottom_frame (react-dom-client.development.js:25989:20)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@vercel
Copy link

vercel bot commented Oct 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
learningmap Ready Ready Preview Comment Oct 13, 2025 10:09pm

…ion deps

Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix maximum update depth exceeded error in component Fix infinite loop in LearningMap component caused by unstable useEffect dependencies Oct 13, 2025
Copilot AI requested a review from mikebarkmin October 13, 2025 21:52
@mikebarkmin mikebarkmin marked this pull request as ready for review October 13, 2025 22:09
@mikebarkmin mikebarkmin merged commit 3e9b130 into main Oct 13, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants