# react-example-unmount

> Properly unmount and remount editor instances.

A shadcn/ui block from the [@prosekit](https://prosekit.dev) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/prosekit/react-example-unmount).

- Page, with a live preview: https://shadcn.exchange/r/prosekit/react-example-unmount
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/prosekit/react-example-unmount-light-3b580069aa78bdb1.png
- Registry item: https://prosekit.dev/r/react-example-unmount.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://prosekit.dev/r/react-example-unmount.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `prosekit`
- Registry items: `https://unpkg.com/prosekit-registry/dist/r/react-ui-inline-menu.json`

## Files

### `registry/src/react/examples/unmount/editor-component.tsx`

```tsx
'use client'

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'

import { defineBasicExtension } from 'prosekit/basic'
import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { InlineMenu } from '../../ui/inline-menu/index.ts'

import ExtensionComponent from './extension-component.tsx'

export default function EditorComponent(props: {
  placeholder: string
}) {
  const editor = useMemo(() => {
    return createEditor({ extension: defineBasicExtension() })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className="box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow-sm flex flex-col bg-[canvas] text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden outline-0 [&_span[data-mention=user]]:text-blue-500 [&_span[data-mention=tag]]:text-violet-500"></div>
          <InlineMenu />
        </div>
      </div>
      <ExtensionComponent placeholder={props.placeholder} />
    </ProseKit>
  )
}
```

### `registry/src/react/examples/unmount/editor.tsx`

```tsx
'use client'

import { useCallback, useRef, useState } from 'react'

import EditorComponent from './editor-component.tsx'

function EditorGroup() {
  const nextKeyRef = useRef(1)
  const [editorKeys, setEditorKeys] = useState<number[]>([])

  const addEditor = useCallback(() => {
    const key = nextKeyRef.current
    nextKeyRef.current += 1
    setEditorKeys((keys) => [...keys, key])
  }, [])

  const removeEditor = useCallback((key: number) => {
    setEditorKeys((keys) => keys.filter((k) => k !== key))
  }, [])

  return (
    <div className="flex flex-col gap-2">
      <div className="flex gap-2">
        <button onClick={addEditor} className="border p-2">
          Add editor
        </button>
        {editorKeys.map((key) => (
          <button
            key={key}
            onClick={() => removeEditor(key)}
            className="border p-2"
          >
            Unmount No.{key}
          </button>
        ))}
      </div>
      {editorKeys.map((key) => (
        <div key={key}>
          <EditorComponent
            placeholder={`Editor No.${key}`}
          />
        </div>
      ))}
    </div>
  )
}

export default EditorGroup
```

### `registry/src/react/examples/unmount/extension-component.tsx`

```tsx
'use client'

import { definePlaceholder } from 'prosekit/extensions/placeholder'
import { useExtension } from 'prosekit/react'
import { useMemo } from 'react'

export default function ExtensionComponent(props: {
  placeholder: string
}) {
  const extension = useMemo(
    () => definePlaceholder({ placeholder: props.placeholder }),
    [props.placeholder],
  )

  useExtension(extension)

  return null
}
```

### `registry/src/react/examples/unmount/index.ts`

```ts
export { default as ExampleEditor } from './editor.tsx'
```
