From aee4e4004c829d059f53f541565eda001ac26f43 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 20 Sep 2021 22:19:07 +0200 Subject: [PATCH 1/3] Only use value prop as initial state --- packages/slate-react/src/components/slate.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/slate-react/src/components/slate.tsx b/packages/slate-react/src/components/slate.tsx index a01b7c6e9b..5557da2f51 100644 --- a/packages/slate-react/src/components/slate.tsx +++ b/packages/slate-react/src/components/slate.tsx @@ -19,8 +19,8 @@ export const Slate = (props: { onChange: (value: Descendant[]) => void }) => { const { editor, children, onChange, value, ...rest } = props - const [key, setKey] = useState(0) - const context: [ReactEditor] = useMemo(() => { + + const [context, setContext] = React.useState<[ReactEditor]>(() => { if (!Node.isNodeList(value)) { throw new Error( `[Slate] value is invalid! Expected a list of elements` + @@ -35,12 +35,11 @@ export const Slate = (props: { editor.children = value Object.assign(editor, rest) return [editor] - }, [key, value, ...Object.values(rest)]) + }) const onContextChange = useCallback(() => { - onChange(editor.children) - setKey(key + 1) - }, [key, onChange]) + setContext([editor]) + }, [onChange]) EDITOR_TO_ON_CHANGE.set(editor, onContextChange) From 6f23c45bb8607dc3e7d817d4dfbe99cb5f883caf Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 20 Sep 2021 22:31:07 +0200 Subject: [PATCH 2/3] Add onChange call back --- packages/slate-react/src/components/slate.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/slate-react/src/components/slate.tsx b/packages/slate-react/src/components/slate.tsx index 5557da2f51..85fb580f42 100644 --- a/packages/slate-react/src/components/slate.tsx +++ b/packages/slate-react/src/components/slate.tsx @@ -38,6 +38,7 @@ export const Slate = (props: { }) const onContextChange = useCallback(() => { + onChange(editor.children) setContext([editor]) }, [onChange]) From 754bed9973a354a8c9f397402aeb723519cfe5e2 Mon Sep 17 00:00:00 2001 From: Bryan Date: Tue, 28 Sep 2021 16:32:40 +0200 Subject: [PATCH 3/3] add changeset --- .changeset/lovely-goats-argue.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lovely-goats-argue.md diff --git a/.changeset/lovely-goats-argue.md b/.changeset/lovely-goats-argue.md new file mode 100644 index 0000000000..efad563fbb --- /dev/null +++ b/.changeset/lovely-goats-argue.md @@ -0,0 +1,5 @@ +--- +'slate-react': minor +--- + +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.