Skip to content

Commit

Permalink
fix(react): useLayoutEffect instead of useEffect to cut down on reflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Nov 12, 2024
1 parent 942fd07 commit 7de99c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-pears-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/react": patch
---

This changes useEditorState to use the useLayoutEffect hook instead of the useEffect hook, so that state that might render to the page can be committed in one pass instead of two.
4 changes: 2 additions & 2 deletions packages/react/src/useEditorState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Editor } from '@tiptap/core'
import deepEqual from 'fast-deep-equal/es6/react'
import { useDebugValue, useEffect, useState } from 'react'
import { useDebugValue, useLayoutEffect, useState } from 'react'
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'

export type EditorStateSnapshot<TEditor extends Editor | null = Editor | null> = {
Expand Down Expand Up @@ -164,7 +164,7 @@ export function useEditorState<TSelectorResult>(
options.equalityFn ?? deepEqual,
)

useEffect(() => {
useLayoutEffect(() => {
return editorStateManager.watch(options.editor)
}, [options.editor, editorStateManager])

Expand Down

0 comments on commit 7de99c3

Please sign in to comment.