Skip to content

Instantly share code, notes, and snippets.

@itsmunim
Created September 28, 2022 02:15
Show Gist options
  • Save itsmunim/d05166295908336800f14abd496d50ff to your computer and use it in GitHub Desktop.
Save itsmunim/d05166295908336800f14abd496d50ff to your computer and use it in GitHub Desktop.
Sample of a widget library rendering
import { renderToString } from 'react-dom/server';
import { createElement } from 'react';
import * as allWidgets from 'widget-library';
// POST /api/render
const render = (req, res) => {
const { widgetName, props } = req.body;
if (!(widgetName in allWidgets)) {
return res.status(404).json({ error: `Widget with name ${widgetName} not found.` });
}
const Widget = allWidgets[widget];
const instance = createElement(Widget, props);
res.status(200).json({ rendered: renderToString(instance) });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment