Skip to content

Commit

Permalink
fix(structure): set patchRef in an insertion effect instead of regula…
Browse files Browse the repository at this point in the history
…r useEffect
  • Loading branch information
bjoerge committed Jan 6, 2025
1 parent 2deeee0 commit 94d39e5
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {useToast} from '@sanity/ui'
import {fromString as pathFromString, pathFor, resolveKeyedPath} from '@sanity/util/paths'
import {omit, throttle} from 'lodash'
import {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'
import {memo, useCallback, useEffect, useInsertionEffect, useMemo, useRef, useState} from 'react'
import deepEquals from 'react-fast-compare'
import {
type DocumentFieldAction,
Expand Down Expand Up @@ -297,10 +297,13 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {

const patchRef = useRef<(event: PatchEvent) => void>(() => {
throw new Error(
'Attempted to patch the Sanity document during initial render. Input components should only call `onChange()` in an effect or a callback.',
'Attempted to patch the Sanity document during initial render or in an `useInsertionEffect`. Input components should only call `onChange()` in a useEffect or an event handler.',
)
})
useEffect(() => {
useInsertionEffect(() => {
// note: this needs to happen in an insertion effect to make sure we're ready to receive patches from child components when they run their effects initially
// in case they do e.g. `useEffect(() => props.onChange(set("foo")), [])`
// Note: although we discourage patch-on-mount, we still support it.
patchRef.current = (event: PatchEvent) => {
// when creating a new draft
if (!editState.draft && !editState.published) {
Expand Down

0 comments on commit 94d39e5

Please sign in to comment.