slate-react: empty editor state array throws error

Created on 15 Apr 2020  路  3Comments  路  Source: ianstormtaylor/slate

const [value, setValue] = useState<Node[]>([]);

This should work, I guess (but throws an error). I need to add

const initialValue = [{
    children: [{text: ''}]
}];

const [value, setValue] = useState<Node[]>(initialValue);

to show an empty editor. Is that intented?

discussion

Most helpful comment

@CameronAckermanSEL would it make sense to add a note to the "Installing Slate" document? I assumed each step would work in isolation and was confused why it didn't until I found this issue.

For example

const App = () => {
  const editor = useMemo(() => withReact(createEditor()), [])
  const [value, setValue] = useState([])
  return (
    // Add the editable component inside the context.
    <Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
      <Editable />
    </Slate>
  )
}

All 3 comments

In my opinion editor should contain at least one block element

Hi @Obiwarn , this gets reported periodically and the fix is to have the editor contain one element. I don't see this behavior changing because otherwise a user can't put focus inside the editor.

Going to close, but feel free to continue a discussion.

@CameronAckermanSEL would it make sense to add a note to the "Installing Slate" document? I assumed each step would work in isolation and was confused why it didn't until I found this issue.

For example

const App = () => {
  const editor = useMemo(() => withReact(createEditor()), [])
  const [value, setValue] = useState([])
  return (
    // Add the editable component inside the context.
    <Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
      <Editable />
    </Slate>
  )
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ianstormtaylor picture ianstormtaylor  路  3Comments

bunterWolf picture bunterWolf  路  3Comments

chriserickson picture chriserickson  路  3Comments

ianstormtaylor picture ianstormtaylor  路  3Comments

gorillatron picture gorillatron  路  3Comments