slate-react crashes when slate is used as an uncontrolled component with defaultValue

Created on 6 May 2020  路  3Comments  路  Source: ianstormtaylor/slate

Do you want to request a _feature_ or report a _bug_?

bug

What's the current behavior?

App crashes when using with react-slate.

TypeError: Cannot read property 'length' of undefined
    at Editable (.../node_modules/slate-react/dist/index.js:1807:38)

Which is this line

if (placeholder && editor.children.length === 1 && Array.from(slate.Node.texts(editor)).length === 1 && slate.Node.string(editor) === '') {

Children is undefined at this point.

When I remove the placeholder as a prop (to bypass this line) the app crashes differently

Error: Unable to find the path for Slate node: {"operations":[],"selection":null,"marks":null,"defaultValue":[{"children":[{"text":"This is editable plain text!","marks":[]}]}]}
    at Object.findPath (.../node_modules/slate-react/dist/index.js:291:11)```

Sandbox: https://codesandbox.io/s/slate-reproductions-n58jf?file=/index.js
Image: https://i.imgur.com/D1GqTvZ.png

Slate: 0.58.0
Browser: Chrome
OS: Windows

What's the expected behavior?

Most helpful comment

@TedChenNZ where did you find the example? If you change defaultValue to value the example renders
https://codesandbox.io/s/slate-reproductions-qslr2?file=/index.js

If you refer to https://github.com/ianstormtaylor/slate/blob/master/Changelog.md#0510--december-5-2019 you must now manage the state yourself

All 3 comments

@TedChenNZ where did you find the example? If you change defaultValue to value the example renders
https://codesandbox.io/s/slate-reproductions-qslr2?file=/index.js

If you refer to https://github.com/ianstormtaylor/slate/blob/master/Changelog.md#0510--december-5-2019 you must now manage the state yourself

@TedChenNZ , Slate does not at this version support usage in an uncontrolled way, and does not support the defaultValue prop. You need to pass a value to the value prop, and then pass an onChange handler in order to receive new value states from the editor.

I'm going to close this issue as this is not defective behavior. If you have any questions or comments, please feel free to post in this issue.

@CameronAckermanSEL that makes sense! I thought it did because the codepen in the issue template wasn't controlled.

I've updated the codepen here with the latest version and with value/onChange used instead of defaultValue if you wanted to use it: https://codesandbox.io/s/slate-reproductions-c7gyg

Created a PR here https://github.com/ianstormtaylor/slate/pull/3659

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexeiAndreev picture AlexeiAndreev  路  3Comments

bunterWolf picture bunterWolf  路  3Comments

markolofsen picture markolofsen  路  3Comments

chriserickson picture chriserickson  路  3Comments

ezakto picture ezakto  路  3Comments