Persistence in tldraw means storing information about the editor's state to a database and then restoring it later. There are a few options that developers have for getting data into tldraw and out again.

The "persistenceKey" prop

Both the <Tldraw> or <TldrawEditor> components support local persitence and cross-tab synchronization via the persistenceKey prop. Passing a value to this prop will persist the contents of the editor locally to the browser's IndexedDb.

Using a persistenceKey will synchronize data automatically with any other tldraw component with the same persistenceKey prop, even if that component is in a different browser tab.

In the example above, both editors would synchronize their document locally. They would still have two independent instance states (e.g. selections) but the document would be kept in sync and persisted under the same key.

Snapshots

You can get a JSON snapshot of the editor's content using the Editor.store's Store.getSnapshot method.

You can load the snapshot into a new editor with Store.loadSnapshot.

A snapshot includes both the store's serialized records and its serialized schema, which is used for migrations.

By default, the getSnapshot method returns only the editor's document data. If you want to get records from a different scope, You can pass in session, document, presence, or else all for all scopes.

Note that loading a snapshot does not reset the editor's in memory state or UI state. For example, loading a snapshot during a resizing operation may lead to a crash. This is because the resizing state maintains its own cache of information about which shapes it is resizing, and its possible that those shapes may no longer exist!

The "store" prop

While it's possible to load the editor and then load data into its store, we've found it best to create the store, set its data, and then pass the store into the editor.

The store property of the <Tldraw> / <TldrawEditor> components accepts a store that you've defined outside of the component.

Sometimes you won't be able to access the store's data synchronously. To handle this case, the store property also accepts a TLStoreWithStatus.

For a good example of this pattern, see the yjs-example.

Edit this page
Last edited on 22 March 2023
ToolsUser Interface