Created
April 30, 2020 07:39
-
-
Save sqndr/c13777135f4d9753e7d57d1dcc9b6c65 to your computer and use it in GitHub Desktop.
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