Installing Slate
Slate is a monorepo divided up into multi npm packages, so to install it you do:
You'll also need to be sure to install Slate's peer dependencies:
Note, if you'd rather use a pre-bundled version of Slate, you can npm install slate
and retrieve the bundled dist/slate.js
file! Check out the Using the Bundled Source guide for more information.
Once you've installed Slate, you'll need to import it.
Slate exposes a set of modules that you'll use to build your editor. The most important of which is an Editor
component.
In addition to rendering the editor, you need to give Slate a "initial value" to render as content. We'll use the Value
model that ships with Slate to create a new initial value that just contains a single paragraph block with some text in it:
And now that we've our initial value, we define our App
and pass it into Slate's Editor
component, like so:
You'll notice that the onChange
handler passed into the Editor
component just updates the app's state with the newest changed value. That way, when it re-renders the editor, the new value is reflected with your changes.
And that's it!
That's the most basic example of Slate. If you render that onto the page, you should see a paragraph with the text A line of text in a paragraph.
. And when you type, you should see the text change!
Next: Adding Event Handlers
Last updated