Last active
January 20, 2018 05:03
-
-
Save pavelvlasov/fb18a48a917086d1768ea95b378b2c28 to your computer and use it in GitHub Desktop.
use js dom to render react components in node.js
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 { createDomForChart } from "../utils"; | |
export default (data: Data, options: Options = {}): string => { | |
const { target, dom } = createDomForChart(); | |
render(<Bars data={data} options={options} />, target); | |
return dom.serialize(); | |
}; |
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 * as jsdom from "jsdom"; | |
import { DOMElement } from "react"; | |
export function createDomForChart(): { | |
dom: jsdom.JSDOM; | |
target: DOMElement<any, any>; | |
} { | |
const dom = new jsdom.JSDOM(`<body></body>`); | |
const div: DOMElement<any, any> = dom.window.document.createElement("div"); | |
dom.window.document.body.appendChild(div); | |
return { | |
target: div, | |
dom | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment