0.0.3 • Published 3 years ago

forgo-render-to-string v0.0.3

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

forgo-render-to-string

NPM travis-ci

Render JSX and Frogo components to an HTML string.

Works in Node & the browser, making it useful for universal/isomorphic rendering.

>> Cute Fox-Related Demo (@ CodePen) <<


Render JSX/VDOM to HTML

import render from 'forgo-render-to-string';

let vdom = <div class="foo">content</div>;

let html = render(vdom);
console.log(html);
// <div class="foo">content</div>

Render Forgo Components to HTML

import render from 'forgo-render-to-string';

const Box = () => {
	return {
		render: ({ type, children }) => (
			<div class={`box box-${type}`}>{children}</div>
		)
	};
};

let html = render(
	<Box type="open">
		<Fox name="Finn" />
	</Box>
);

console.log(html);
// <div class="box box-open"><span class="fox">Finn</span></div>

Render JSX / Forgo / Whatever via Express!

import express from 'express';
import render from 'forgo-render-to-string';

// silly example component:
const Fox = () => {
	return {
		render: ({ name }) => (
			<div class="fox">
				<h5>{name}</h5>
				<p>This page is all about {name}.</p>
			</div>
		)
	};
};

// basic HTTP server via express:
const app = express();
app.listen(8080);

// on each request, render and return a component:
app.get('/:fox', (req, res) => {
	let html = render(<Fox name={req.params.fox} />);
	// send it back wrapped up as an HTML5 document:
	res.send(`<!DOCTYPE html><html><body>${html}</body></html>`);
});

License

MIT

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago