Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types'
import { forwardRef, useEffect } from 'react'
import { Link } from 'react-router-dom'
import { Link, useLocation } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'

// material-ui
Expand Down Expand Up @@ -77,23 +77,23 @@ const NavItem = ({ item, level, navType, onClick, onUploadFile }) => {
}
}

// active menu item on page load
const location = useLocation()

useEffect(() => {
if (navType === 'MENU') {
const currentIndex = document.location.pathname
.toString()
.split('/')
.findIndex((id) => id === item.id)
if (currentIndex > -1) {
dispatch({ type: MENU_OPEN, id: item.id })
}
if (!document.location.pathname.toString().split('/')[1]) {
itemHandler('chatflows')
}
if (navType !== 'MENU') return

const pathnameParts = location.pathname.toString().split('/')
const activePathSegment = pathnameParts[1]

if (!activePathSegment) {
itemHandler('chatflows')
return
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [navType])
if (activePathSegment === item.id) {
dispatch({ type: MENU_OPEN, id: item.id })
}
}, [dispatch, item.id, location.pathname, navType, itemHandler])
Comment on lines 82 to +96

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

An infinite loop is triggered when navigating to the root path (/).

Cause

  1. itemHandler is a standard function recreated on every render.
  2. Since itemHandler is included in the useEffect dependency array, any re-render triggers the effect.
  3. When on the root path (/), !activePathSegment is true, so itemHandler('chatflows') is called.
  4. This dispatches Redux actions, updating the Redux store.
  5. The Redux state update triggers a re-render of NavItem (via useSelector), recreating itemHandler and triggering the useEffect again, resulting in an infinite loop.

Solution

We can simplify the logic by defaulting activePathSegment to 'chatflows' when it is empty, and only dispatching MENU_OPEN when activePathSegment === item.id. This completely removes the need to call itemHandler inside useEffect, removing it from the dependency array and eliminating the infinite loop entirely. It also avoids redundant dispatches from other NavItem components.

    useEffect(() => {
        if (navType !== 'MENU') return

        const pathnameParts = location.pathname.toString().split('/')
        const activePathSegment = pathnameParts[1] || 'chatflows'

        if (activePathSegment === item.id) {
            dispatch({ type: MENU_OPEN, id: item.id })
        }
    }, [dispatch, item.id, location.pathname, navType])


return (
<ListItemButton
Expand Down