Walkthroughs
Installing Slate from Archbee
13min
slate is a monorepo divided up into multiple npm packages, so to install it you do yarn add slate slate react you'll also need to be sure to install slate's peer dependencies yarn add react react dom note, if you'd rather use a pre bundled version of slate, you can and retrieve the bundled file! check out the docid 1dam8eump zvisuqxf4pb guide for more information once you've installed slate, you'll need to import it // import react dependencies import react, { useeffect, usememo, usestate } from 'react' // import the slate editor factory import { createeditor } from 'slate' // import the slate components and react plugin import { slate, editable, withreact } from 'slate react' before we use those imports, let's start with an empty \<app> component // define our app const app = () => { return null } the next step is to create a new editor object we want the editor to be stable across renders, so we use the usememo hook const app = () => { // create a slate editor object that won't change across renders const editor = usememo(() => withreact(createeditor()), \[]) return null } of course we haven't rendered anything, so you won't see any changes if you are using typescript, you will also need to extend the editor with reacteditor as per the documentation on typescript docid 345hnf9xko4uhhhnt2kxx the example below also includes the custom types required for the rest of this example // typescript users only add this code import { baseeditor } from 'slate' import { reacteditor } from 'slate react' type customelement = { type 'paragraph'; children customtext\[] } type customtext = { text string } declare module 'slate' { interface customtypes { editor baseeditor & reacteditor element customelement text customtext } } next we want to create state for value const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) // keep track of state for the value of the editor const \[value, setvalue] = usestate(\[]) return null } next up is to render a \<slate> context provider the provider component keeps track of your slate editor, its plugins, its value, its selection, and any changes that occur it must be rendered above any \<editable> components but it can also provide the editor state to other components like toolbars, menus, etc using the useslate hook const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) const \[value, setvalue] = usestate(\[]) // render the slate context return ( \<slate editor={editor} value={value} onchange={newvalue => setvalue(newvalue)} /> ) } you can think of the \<slate> component as providing a "controlled" context to every component underneath it this is a slightly different mental model than things like \<input> or \<textarea> , because richtext documents are more complex you'll often want to include toolbars, or live previews, or other complex components next to your editable content by having a shared context, those other components can execute commands, query the editor's state, etc okay, so the next step is to render the \<editable> component itself 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> ) } the \<editable> component acts like contenteditable anywhere you render it will render an editable richtext document for the nearest editor context there's only one last step so far we've been using an empty \[] array as the initial value of the editor, so it has no content let's fix that by defining an initial value the value is just plain json here's one containing a single paragraph block with some text in it const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) // add the initial value when setting up our state const \[value, setvalue] = usestate(\[ { type 'paragraph', children \[{ text 'a line of text in a paragraph ' }], }, ]) return ( \<slate editor={editor} value={value} onchange={newvalue => setvalue(newvalue)} \> \<editable /> \</slate> ) } there you have 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! slate is a monorepo divided up into multiple npm packages, so to install it you do yarn add slate slate react you'll also need to be sure to install slate's peer dependencies yarn add react react dom note, if you'd rather use a pre bundled version of slate, you can and retrieve the bundled file! check out the docid 1dam8eump zvisuqxf4pb guide for more information once you've installed slate, you'll need to import it // import react dependencies import react, { useeffect, usememo, usestate } from 'react' // import the slate editor factory import { createeditor } from 'slate' // import the slate components and react plugin import { slate, editable, withreact } from 'slate react' before we use those imports, let's start with an empty \<app> component // define our app const app = () => { return null } the next step is to create a new editor object we want the editor to be stable across renders, so we use the usememo hook const app = () => { // create a slate editor object that won't change across renders const editor = usememo(() => withreact(createeditor()), \[]) return null } of course we haven't rendered anything, so you won't see any changes if you are using typescript, you will also need to extend the editor with reacteditor as per the documentation on typescript docid 345hnf9xko4uhhhnt2kxx the example below also includes the custom types required for the rest of this example // typescript users only add this code import { baseeditor } from 'slate' import { reacteditor } from 'slate react' type customelement = { type 'paragraph'; children customtext\[] } type customtext = { text string } declare module 'slate' { interface customtypes { editor baseeditor & reacteditor element customelement text customtext } } next we want to create state for value const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) // keep track of state for the value of the editor const \[value, setvalue] = usestate(\[]) return null } next up is to render a \<slate> context provider the provider component keeps track of your slate editor, its plugins, its value, its selection, and any changes that occur it must be rendered above any \<editable> components but it can also provide the editor state to other components like toolbars, menus, etc using the useslate hook const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) const \[value, setvalue] = usestate(\[]) // render the slate context return ( \<slate editor={editor} value={value} onchange={newvalue => setvalue(newvalue)} /> ) } you can think of the \<slate> component as providing a "controlled" context to every component underneath it this is a slightly different mental model than things like \<input> or \<textarea> , because richtext documents are more complex you'll often want to include toolbars, or live previews, or other complex components next to your editable content by having a shared context, those other components can execute commands, query the editor's state, etc okay, so the next step is to render the \<editable> component itself 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> ) } the \<editable> component acts like contenteditable anywhere you render it will render an editable richtext document for the nearest editor context there's only one last step so far we've been using an empty \[] array as the initial value of the editor, so it has no content let's fix that by defining an initial value the value is just plain json here's one containing a single paragraph block with some text in it const app = () => { const editor = usememo(() => withreact(createeditor()), \[]) // add the initial value when setting up our state const \[value, setvalue] = usestate(\[ { type 'paragraph', children \[{ text 'a line of text in a paragraph ' }], }, ]) return ( \<slate editor={editor} value={value} onchange={newvalue => setvalue(newvalue)} \> \<editable /> \</slate> ) } there you have 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!
🤔
Have a question?
Our super-smart AI,knowledgeable support team and an awesome community will get you an answer in a flash.
To ask a question or participate in discussions, you'll need to authenticate first.