slate icon indicating copy to clipboard operation
slate copied to clipboard

`value` should be renamed to `initialValue`

Open macrozone opened this issue 2 years ago • 2 comments

Problem

https://github.com/ianstormtaylor/slate/pull/4922 introduced a massive breaking change for consumers of slate, e.g. for https://github.com/react-page/react-page/issues/1188

Problem is that the meaning of value has changed, slate is unfortunatly no longer a controlled component (which was a far better design overall).

Solution

value needs to be renamed to initialValue to avoid confusion. As slate is still on a 0.x version, this should be done in a breaking change as https://github.com/ianstormtaylor/slate/pull/4922 was already breaking.

this makes a clear cut and opens up better apis to partially restore the old, more powerful api.

Alternatives

keeping value as it is is not a good idea. This will just spread further confusion. When its named value it should be controlled, uncontrolled should not be named value.

Context

You can restore the old behavior by setting editor.children, but it not always rerenders properly.

Update:

I managed to update ReactPage to use editor.children and could remove some workarounds which were probably there because of problems related to syncing our state to the state of slate. Its still a bit ugly, but its a bit simpler then before.

We have similar challenges in ReactPage surrounding syncing the passed value to the editor state in a "controlled" manner, so i totally understand the struggles. I still think following "the principle of least surprise" would either demand that value is renamed to initialValue and/or (much better) value controlling the state like before.

macrozone avatar May 16 '22 10:05 macrozone

I'm new to Slate, and I'm guessing this is the reason why I can't get the onChange prop to work ?

I made a simple codebox example of what I'm trying to do.

Does this mean the deserialisation should always be done before rendering the editor ?

doei avatar Jul 19 '22 12:07 doei

I'm new to Slate, and I'm guessing this is the reason why I can't get the onChange prop to work ?

I made a simple codebox example of what I'm trying to do.

Does this mean the deserialisation should always be done before rendering the editor ?

kindof yes, its really sad that slate is now a uncontrolled component. That will lead to a lot of unexpected bugs.

In your codebox, this workaround works:



import { Slate, Editable, withReact, ReactEditor } from "slate-react";

// in your useEffect ...

    console.log("setting value", res);
        editor.children = res.result;
        ReactEditor.focus(editor);

macrozone avatar Jul 19 '22 14:07 macrozone

I render two editors that share one value, and want to make changes in one editor while the other editor synchronizes the changes.

I tried

useEffect(() => {
  editor.children = value;
}, [editor, value]);

it will always be one letter slower in another editor.

I also tried to add a key={JSON.stringify(value)} to <Slate>, which effectively synchronizes the value of the two editors, and the cursor appears to have no errors. But I don't think it's reliable.

Is there no better way at present?😂

I found out someone else did it too, https://github.com/ianstormtaylor/slate/issues/4612#issuecomment-1348310378

sws797 avatar Mar 22 '23 09:03 sws797