Skip to content

Commit

Permalink
Double character insert regression (#4460)
Browse files Browse the repository at this point in the history
* fix double character regression

* add changeset
  • Loading branch information
dylans committed Aug 19, 2021
1 parent 2fa9281 commit ace397f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-windows-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

fix double character insertion regression due to unnecessary memo
40 changes: 17 additions & 23 deletions packages/slate-react/src/components/string.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,26 @@ const String = (props: {
/**
* Leaf strings with text in them.
*/
const TextString = React.memo(
(props: { text: string; isTrailing?: boolean }) => {
const { text, isTrailing = false } = props
const TextString = (props: { text: string; isTrailing?: boolean }) => {
const { text, isTrailing = false } = props

const ref = useRef<HTMLSpanElement>(null)
const forceUpdateFlag = useRef(false)
const ref = useRef<HTMLSpanElement>(null)
const forceUpdateFlag = useRef(false)

if (ref.current && ref.current.textContent !== text) {
forceUpdateFlag.current = !forceUpdateFlag.current
}

// This component may have skipped rendering due to native operations being
// applied. If an undo is performed React will see the old and new shadow DOM
// match and not apply an update. Forces each render to actually reconcile.
return (
<span
data-slate-string
ref={ref}
key={forceUpdateFlag.current ? 'A' : 'B'}
>
{text}
{isTrailing ? '\n' : null}
</span>
)
if (ref.current && ref.current.textContent !== text) {
forceUpdateFlag.current = !forceUpdateFlag.current
}
)

// This component may have skipped rendering due to native operations being
// applied. If an undo is performed React will see the old and new shadow DOM
// match and not apply an update. Forces each render to actually reconcile.
return (
<span data-slate-string ref={ref} key={forceUpdateFlag.current ? 'A' : 'B'}>
{text}
{isTrailing ? '\n' : null}
</span>
)
}

/**
* Leaf strings without text, render as zero-width strings.
Expand Down

0 comments on commit ace397f

Please sign in to comment.