Skip to content

Instantly share code, notes, and snippets.

@pavelvlasov
Last active January 20, 2018 05:03
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 pavelvlasov/fb18a48a917086d1768ea95b378b2c28 to your computer and use it in GitHub Desktop.
Save pavelvlasov/fb18a48a917086d1768ea95b378b2c28 to your computer and use it in GitHub Desktop.
use js dom to render react components in node.js
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();
};
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