Skip to content

Instantly share code, notes, and snippets.

@omichelsen
Created January 29, 2020 00:31
Show Gist options
  • Save omichelsen/dd9dd28f81b19b121fe78a45b3057a76 to your computer and use it in GitHub Desktop.
Save omichelsen/dd9dd28f81b19b121fe78a45b3057a76 to your computer and use it in GitHub Desktop.
Creates a function that will render a React component into a target element and return an unmount function.
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
/**
* Creates a function that will render a React component into a target element and return an unmount function.
* The entry point should be loaded async by a ReactLoader component to ensure code splitting.
* @param component Root component to render
*/
export default <P>(component: React.ComponentType<P>) => (domElement: Element, props: P) => {
render(React.createElement(component, props), domElement)
return () => {
unmountComponentAtNode(domElement)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment