Replace every instance with react component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { render } from 'react-dom'; | |
const renderComponent = (Component, root) => { | |
const parent = document.createElement('div'); | |
render(Component, parent, () => { | |
root.replaceWith(...Array.from(parent.childNodes)); | |
}); | |
}; | |
const renderMultiple = (Component, root) => { | |
const containers = document.querySelectorAll(root); | |
if (containers.length) { | |
Array.from(containers).map(container => renderComponent( | |
Component, | |
container, | |
)); | |
} | |
}; | |
export { renderComponent, renderMultiple }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment