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.
<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.
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.
You can load the snapshot into a new editor with
By default, the
getSnapshotmethod returns only the editor's document data. If you want to get records from a different scope, You can pass in
presence, or else
allfor 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!
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.
store property of the
<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
For a good example of this pattern, see the yjs-example.