Saving to a Database

Now that you've learned the basics of how to add functionality to the Slate editor, you might be wondering how you'd go about saving the content you've been editing, such that you can come back to your app later and have it load.

In this guide, we'll show you how to add logic to save your Slate content to a database for storage and retrieval later.

Let's start with a basic editor:

import { Editor } from 'slate-react'
import { Value } from 'slate'

const initialValue = Value.fromJSON({
document: {
nodes: [
{
kind: 'block',
type: 'paragraph',
nodes: [
{
kind: 'text',
leaves: [
{
text: 'A line of text in a paragraph.'
}
]
}
]
}
]
}
})

class App extends React.Component {

state = {
value: initialValue
}

onChange = ({ value }) => {
this.setState({ value })
}

render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
/>
)
}

}

That will render a basic Slate editor on your page, and when you type things will change. But if you refresh the page, everything will be reverted back to its original value—nothing saves!

What we need to do is save the changes you make somewhere. For this example, we'll just be using Local Storage, but it will give you an idea for where you'd need to add your own database hooks.

So, in our onChange handler, we need to save the value. But the value argument that onChange receives is an immutable object, so we can't just save it as-is. We need to serialize it to a format we understand first, like JSON!

const initialValue = Value.fromJSON({
document: {
nodes: [
{
kind: 'block',
type: 'paragraph',
nodes: [
{
kind: 'text',
leaves: [
{
text: 'A line of text in a paragraph.'
}
]
}
]
}
]
}
})

class App extends React.Component {

state = {
value: initialValue
}

onChange = ({ value }) => {
// Save the value to Local Storage.
const content = JSON.stringify(value.toJSON())
localStorage.setItem('content', content)

this.setState({ value })
}

render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
/>
)
}

}

Now whenever you edit the page, if you look in Local Storage, you should see the content value changing.

But... if you refresh the page, everything is still reset. That's because we need to make sure the initial value is pulled from that same Local Storage location, like so:

// Update the initial content to be pulled from Local Storage if it exists.
const existingValue = JSON.parse(localStorage.getItem('content'))
const initialValue = Value.fromJSON(existingValue || {
document: {
nodes: [
{
kind: 'block',
type: 'paragraph',
nodes: [
{
kind: 'text',
leaves: [
{
text: 'A line of text in a paragraph.'
}
]
}
]
}
]
}
})

class App extends React.Component {

state = {
value: initialValue
}

onChange = ({ value }) => {
const content = JSON.stringify(value.toJSON())
localStorage.setItem('content', content)

this.setState({ value })
}

render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
/>
)
}

}

Now you should be able to save changes across refreshes!

However, if you inspect the change handler, you'll notice that it's actually saving the Local Storage value on every change to the editor, even when only the selection changes! This is because onChange is called for every change. For Local Storage this doesn't really matter, but if you're saving things to a database via HTTP request this would result in a lot of unnecessary requests. You can fix this by checking against the previous document value.

const existingValue = JSON.parse(localStorage.getItem('content'))
const initialValue = Value.fromJSON(existingValue || {
document: {
nodes: [
{
kind: 'block',
type: 'paragraph',
nodes: [
{
kind: 'text',
leaves: [
{
text: 'A line of text in a paragraph.'
}
]
}
]
}
]
}
})

class App extends React.Component {

state = {
value: initialValue
}

onChange = ({ value }) => {
// Check to see if the document has changed before saving.
if (value.document != this.state.value.document) {
const content = JSON.stringify(value.toJSON())
localStorage.setItem('content', content)
}

this.setState({ value })
}

render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
/>
)
}

}

Now you're content will be saved only when the content itself changes!

Success—you've got JSON in your database.

But what if you want something other than JSON? Well, you'd need to serialize your value differently. For example, if you want to save your content as plain text instead of JSON, you can use the Plain serializer that ships with Slate, like so:

// Switch to using the Plain serializer.
import { Editor } from 'slate-react'
import Plain from 'slate-plain-serializer'

const existingValue = localStorage.getItem('content')
const initialValue = Plain.deserialize(existingValue || 'A string of plain text.')

class App extends React.Component {

state = {
value: initialValue
}

onChange = ({ value }) => {
if (value.document != this.state.value.document) {
const content = Plain.serialize(value)
localStorage.setItem('content', content)
}

this.setState({ value })
}

render() {
return (
<Editor
value={this.state.value}
onChange={this.onChange}
/>
)
}

}

That works! Now you're working with plain text.

However, sometimes you may want something a bit more custom, and a bit more complex... good old fashioned HTML. In that case, check out the next guide...

Was this page helpful? Let us know how we did: