Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unsafe ref access during render #8054

Draft
wants to merge 1 commit into
base: add-million-lint-mode
Choose a base branch
from
Draft
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,4 +1,4 @@
import {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'
import {memo, useCallback, useEffect, useMemo, useState} from 'react'
import {type DocumentInspector, type DocumentInspectorMenuItem, useUnique} from 'sanity'

interface DocumentInspectorMenuItemsResolverProps {
Expand All @@ -18,36 +18,26 @@ export function DocumentInspectorMenuItemsResolver(props: DocumentInspectorMenuI
const {documentId, documentType, inspectors, onMenuItems} = props

const len = inspectors.length
const lenRef = useRef(len)

const [menuItems, setMenuItems] = useState<DocumentInspectorMenuItem[]>(() =>
Array.from(new Array(len)),
)

const menuItemsRef = useRef(menuItems)

useEffect(() => {
if (lenRef.current !== len) {
if (menuItems.length !== len) {
const newFieldActions = Array.from(new Array(len))

for (let i = 0; i < len; i++) {
newFieldActions[i] = menuItemsRef.current[i]
newFieldActions[i] = menuItems[i]
}

lenRef.current = len

setMenuItems(() => {
menuItemsRef.current = newFieldActions
return newFieldActions
})
setMenuItems(newFieldActions)
}
}, [len])
}, [len, menuItems])

const setMenuItem = useCallback((index: number, node: DocumentInspectorMenuItem) => {
setMenuItems((prev) => {
const next = [...prev]
next[index] = node
menuItemsRef.current = next
return next
})
}, [])
Expand Down
Loading