Created
September 28, 2022 02:15
-
-
Save itsmunim/d05166295908336800f14abd496d50ff to your computer and use it in GitHub Desktop.
Sample of a widget library rendering
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 { 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