From b9c9ab07ec0d4e93ad898620f7a4c6e7f8e82250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Fri, 3 Jan 2025 10:18:07 +0100 Subject: [PATCH] fix(sanity): throw a slightly(!) more helpful error message if calling onChange during initial render --- .../sanity/playwright-ct/tests/formBuilder/utils/TestForm.tsx | 4 +++- .../components/form/tasksFormBuilder/useTasksFormBuilder.ts | 4 +++- .../src/structure/panes/document/DocumentPaneProvider.tsx | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/sanity/playwright-ct/tests/formBuilder/utils/TestForm.tsx b/packages/sanity/playwright-ct/tests/formBuilder/utils/TestForm.tsx index 96b71978868..2603d5d2176 100644 --- a/packages/sanity/playwright-ct/tests/formBuilder/utils/TestForm.tsx +++ b/packages/sanity/playwright-ct/tests/formBuilder/utils/TestForm.tsx @@ -186,7 +186,9 @@ export function TestForm(props: TestFormProps) { }, [setFocusPath]) const patchRef = useRef<(event: PatchEvent) => void>(() => { - throw new Error('Nope') + throw new Error( + 'Attempted to patch the Sanity document during initial render. Input components should only call `onChange()` in an effect or a callback.', + ) }) patchRef.current = (event: PatchEvent) => { diff --git a/packages/sanity/src/core/tasks/components/form/tasksFormBuilder/useTasksFormBuilder.ts b/packages/sanity/src/core/tasks/components/form/tasksFormBuilder/useTasksFormBuilder.ts index 9a5d1cc6ae9..37001003c45 100644 --- a/packages/sanity/src/core/tasks/components/form/tasksFormBuilder/useTasksFormBuilder.ts +++ b/packages/sanity/src/core/tasks/components/form/tasksFormBuilder/useTasksFormBuilder.ts @@ -81,7 +81,9 @@ export function useTasksFormBuilder(options: TasksFormBuilderOptions): TasksForm const {patch} = useDocumentOperation(documentId, documentType) const patchRef = useRef<(event: PatchEvent) => void>(() => { - throw new Error('Nope') + throw new Error( + 'Attempted to patch the Sanity document during initial render. Input components should only call `onChange()` in an effect or a callback.', + ) }) useEffect(() => { patchRef.current = (event: PatchEvent) => { diff --git a/packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx b/packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx index 8f85d2d74d4..d35ec275dad 100644 --- a/packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx +++ b/packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx @@ -296,7 +296,9 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => { ) const patchRef = useRef<(event: PatchEvent) => void>(() => { - throw new Error('Nope') + throw new Error( + 'Attempted to patch the Sanity document during initial render. Input components should only call `onChange()` in an effect or a callback.', + ) }) useEffect(() => { patchRef.current = (event: PatchEvent) => {