Displaying Selected Blocks

Displaying Selected Blocks

In this example, the JSON representation of blocks spanned by the user's selection, is displayed below the editor.

Try it out: Select different blocks in the editor and see the JSON update!

Relevant Docs:

import { Block } from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView, useCreateBlockNote } from "@blocknote/react";
import "@blocknote/react/style.css";
import { useState } from "react";
import "./styles.css";
export default function App() {
  // Stores the selected blocks as an array of Block objects.
  const [blocks, setBlocks] = useState<Block[]>([]);
  // Creates a new editor instance.
  const editor = useCreateBlockNote({
    initialContent: [
        type: "paragraph",
        content: "Welcome to this demo!",
        type: "paragraph",
        content: "Select different blocks to see the JSON change below",
        type: "paragraph",
  // Renders the editor instance.
  return (
    <div className={"wrapper"}>
      <div>BlockNote Editor:</div>
      <div className={"item"}>
          onSelectionChange={() => {
            const selection = editor.getSelection();
            // Get the blocks in the current selection and store on the state. If
            // the selection is empty, store the block containing the text cursor
            // instead.
            if (selection !== undefined) {
            } else {
      <div>Selection JSON:</div>
      <div className={"item bordered"}>
          <code>{JSON.stringify(blocks, null, 2)}</code>