Skip to content

Instantly share code, notes, and snippets.

@sqndr
Created April 30, 2020 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sqndr/c13777135f4d9753e7d57d1dcc9b6c65 to your computer and use it in GitHub Desktop.
Save sqndr/c13777135f4d9753e7d57d1dcc9b6c65 to your computer and use it in GitHub Desktop.
Replace every instance with react component
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