Rendering a React component inside a node

@marijn: you mean retrieving the react component from that DOM subtree ? That is not needed. Only the DOM node to which the react component has been mounted to is needed for unmounting:

ReactDOM.unmountComponentAtNode(domContainerNode)

and domContainerNode elements can easily be found with a DOM query in the hook, from the root of the subtree to be deleted.

ProseMirror will ‘drop’ whole subtrees at a time, without recursing through them, so an unmount-related callback would not be called for every node, but only for each subtree that’s dropped. So you’d want to scan those in the callback, to find the relevant React components.

Does that sound like it’d work?

Yes, that is fine. In the callback, the React cleanup can be done, for each component within that dropped subtree.

Implemented in this commit, which will be part of the upcoming 0.11.0

1 Like

great, thanks, I look forward to 0.11.0

how could callback be implemented for cleaning in react?

Hey guys - I have been trying to render React components as NodeView too. I have created this repo to show what I am currently doing:

  1. Use a NodeView to render the component
  2. Instead of using ReactDOM.render, I am using ReactDOM.createPortal. I return the portal component to my editor component which then renders it on the page for it to access my app’s context
  3. I’ve wrapped the NodeView in a React context so that its props are available within the component using hooks

Is this how you guys are approaching it too?

1 Like