Skip to content

Commit

Permalink
fix(Presence): replace unsafe ref access during render
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 13, 2024
1 parent 9d075f4 commit 8b6919e
Showing 1 changed file with 7 additions and 4 deletions.
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
}

0 comments on commit 8b6919e

Please sign in to comment.