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(Presence): replace unsafe ref access during render #8048

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
11 changes: 7 additions & 4 deletions packages/sanity/src/core/form/studio/contexts/Presence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {type Path} from '@sanity/types'
import {isEqual, startsWith} from '@sanity/util/paths'
import {type ReactNode, useContext, useRef} from 'react'
import {type ReactNode, useContext, useState} from 'react'
import {PresenceContext} from 'sanity/_singletons'

import {type FormNodePresence} from '../../../presence'
Expand All @@ -29,13 +29,16 @@ export function useFormFieldPresence(): FormNodePresence[] {
*/
export function useChildPresence(path: Path, inclusive?: boolean): FormNodePresence[] {
const presence = useFormFieldPresence()
const prev = useRef(presence)
const [prev, setPrev] = useState(presence)

const next = immutableReconcile(
prev.current,
prev,
presence.filter(
(item) => startsWith(path, item.path) && (inclusive || !isEqual(path, item.path)),
),
)
prev.current = next
if (next !== prev) {
setPrev(next)
}
return next
}
Loading