`toDOM` and event listeners

Is it a good practive to define event listeners in toDOM method like below or I should define custom node view (which comparing to toDOM gives me a getPos method to figure out node position)? Also, I was wondering whether instead of creating node views with specific listeners, I could create a plugin with click listeners which based on target figure out which node was clicked and do specific action. Is it acceptable?

    const dom = document.createElement('img');
    dom.src = node.attrs.src;
    dom.width = node.attrs.width;
    dom.onclick = () => console.log('Hi');
    return { dom };

You can often just use the handleClickOn prop. But if not, since toDOM is also used to render static HTML, a node view is indeed probably a better way to add these.

1 Like