Y-quill change doc without reconnect y-webrtc

it looks it can’t sync quill sometime after switch currentPendingId

useEffect(() => {
    let quill, ydoc, ytext, provider, binding, leave

    const gc = () => {
      try {
        window.removeEventListener('blur', leave)
      } catch (ex) {
        // console.log(ex)

    if (!currentPendingId) {

    quill = textareaRef.current = new Quill(document.querySelector('#detail-editor'), {
      modules: {
        cursors: true,
        toolbar: false,
        history: {
          userOnly: true
      placeholder: 'input your detail..',
      theme: 'bubble', // 'bubble' is also great,

    // A Yjs document holds the shared data
    ydoc = new Y.Doc()

    const indexeddbProvider = new IndexeddbPersistence(currentPendingId, ydoc)

    indexeddbProvider.whenSynced.then(() => {
      console.log('loaded data from indexed db')

    ytext = ydoc.getText('quill')

    const url = Meteor.settings.public.yjsUrl || 'ws://localhost:4444'

    provider = new WebrtcProvider(currentPendingId, ydoc, { signaling: [url] })

    binding = new QuillBinding(ytext, quill, provider.awareness)

    leave = () => { quill.blur() }

    // Remove the selection when the iframe is blurred
    window.addEventListener('blur', leave)

    return () => {
  }, [currentPendingId])

I’m not sure what currentPendingId is but you should not try-catch the destructing at least, since if it fails yjs might be left hanging in the background. I nowadays wait for the provider to trigger sync before rendering the editor also, since if you use schema that does not default to doc with empty paragraph yjs will generate extra paragraphs as a user change eg

        provider.on('synced', () => {
          const pmDoc = yDocToProsemirrorJSON(yDoc, 'prosemirror')
          const data = {

But in your case, you should check you destroy the doc properly. Probably you dont have to destroy the editor if you allow changing the Yjs doc on the fly but that shouldnt matter.