question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Normalization for initial value

See original GitHub issue

Do you want to request a feature or report a bug?

feature

What’s the current behavior?

  1. Value is set on editor
  2. Only when the user types something into the editor, the editor.normalizeNode is called for that specific node (and it’s parent nodes)
  3. Other nodes that have not been touched stay invalid until e.g. the text of that node is edited

Slate: 0.57.1 Browser: Chrome / Safari / Firefox / Edge OS: Mac / Windows / Linux / iOS / Android

What’s the expected behavior?

There are multiple solutions. I currently have 2 in mind:

  1. All nodes are normalized once after the value has been set initially
  2. A callback is exposed (e.g. onValueUpdated) where we could add some logic to run Editor.normalize(editor) everytime the value has been updated from outside (e.g. coming from a DB)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:21
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

21reactions
davidruisingercommented, Jul 8, 2020

After running into other issues with the above workarounds I finally ended up with this:

  1. I have my editor component that receives the value as prop from outside
  2. I store the “internal” slate value in a react state within my editor component
  3. I use useEffect hook to update the internal slate value with the incoming external value

Now here comes the magic part. Instead of just setting the external value on the internal state I use this:

React.useEffect(() => {
    // Slate throws an error if the value on the initial render is invalid
    // so we directly set the value on the editor in order
    // to be able to trigger normalization on the initial value before rendering
    editor.children = externalValue
    Editor.normalize(editor, { force: true })
    // We set the internal value so that the rendering can take over from here
    setInternalSlateValue(editor.children)
}, [externalValue])
5reactions
janpaepkecommented, Mar 1, 2022

The solution by @davidruisinger worked great, as I also was running into troubles when building a controlled slate component - when setting the external value to editor.children it skips normalisation.

However I improved it a little, because in his solution the component would always render twice (once with an initial value, and once, when the internal state is set after the useEffect runs).

You can avoid this by doing it in a useMemo.

const slateValue = useMemo(() => {
	// Slate throws an error if the value on the initial render is invalid
	// so we directly set the value on the editor in order
	// to be able to trigger normalization on the initial value before rendering
	editor.children = externalValue;
	Editor.normalize(editor, { force: true });
	// We return the normalized internal value so that the rendering can take over from here
	return editor.children;
}, [editor, externalValue]);

Then I use this on my Slate Component.

<Slate
	editor={editor}
	value={slateValue} // now normalized
	onChange={onChange} // set / update external value
>

hope this helps someone else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: Normalizing Data to Initial Value - JMP User Community
I have a data set that I am plotting against time, and I was wondering if there was a feature in JMP to...
Read more >
Normalization Formula - WallStreetMojo
The equation for normalization is derived by initially deducting the minimum value from the variable to be normalized. Next, the minimum value deducts...
Read more >
Normalization Formula: How To Use It on a Data Set - Indeed
How to use the normalization formula · 1. Calculate the range of the data set · 2. Subtract the minimum x value from...
Read more >
How to Normalize Data Between 0 and 1 - Statology
To normalize the values in a dataset to be between 0 and 1, you can use the following formula: zi = (xi –...
Read more >
Normalization - Codecademy
Min-max normalization is one of the most common ways to normalize data. For every feature, the minimum value of that feature gets transformed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found