Answer the question
In order to leave comments, you need to log in
How to properly include preact-render-to-string?
The example from Github swears at the syntax. How to properly include preact-render-to-string? https://github.com/developit/preact-render-to-string
Error: SyntaxError: Unexpected token import
If changed to require, then swears at JSX tags `<`
Code:
import express from 'express';
import { h } from 'preact';
import render from 'preact-render-to-string';
/** @jsx h */
// silly example component:
const Fox = ({ 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>`);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question