Skip to content

Commit 11ef83b

Browse files
authored
Only use Slate Provider's value prop as initial state (ianstormtaylor#4540)
* Only use value prop as initial state * Add onChange call back * add changeset
1 parent c29eea0 commit 11ef83b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

.changeset/lovely-goats-argue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate-react': minor
3+
---
4+
5+
The Slate Provider's "value" prop is now only used as initial state for editor.children as was intended before. If your code relies on replacing editor.children you should do so by replacing it directly instead of relying on the "value" prop to do this for you.

packages/slate-react/src/components/slate.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const Slate = (props: {
1919
onChange: (value: Descendant[]) => void
2020
}) => {
2121
const { editor, children, onChange, value, ...rest } = props
22-
const [key, setKey] = useState(0)
23-
const context: [ReactEditor] = useMemo(() => {
22+
23+
const [context, setContext] = React.useState<[ReactEditor]>(() => {
2424
if (!Node.isNodeList(value)) {
2525
throw new Error(
2626
`[Slate] value is invalid! Expected a list of elements` +
@@ -35,12 +35,12 @@ export const Slate = (props: {
3535
editor.children = value
3636
Object.assign(editor, rest)
3737
return [editor]
38-
}, [key, value, ...Object.values(rest)])
38+
})
3939

4040
const onContextChange = useCallback(() => {
4141
onChange(editor.children)
42-
setKey(key + 1)
43-
}, [key, onChange])
42+
setContext([editor])
43+
}, [onChange])
4444

4545
EDITOR_TO_ON_CHANGE.set(editor, onContextChange)
4646

0 commit comments

Comments
 (0)