import React from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import { renderToString } from "https://esm.sh/react-dom/server";
function Counter() {
const [count, setCount] = React.useState(0);
return (
<button onClick={() => setCount(count + 1)}>
I've been clicked {count} times
</button>
);
}
if (typeof document !== "undefined") {
createRoot(document.getElementById("root")).render(<Counter />);
}
export default async function(req: Request): Promise<Response> {
return new Response(
renderToString(
<html>
<body>
<h1>Val Town React Client Demo</h1>
<div id="root"></div>
</body>
<script type="module" src={import.meta.url}></script>
</html>,
),
{
headers: {
"content-type": "text/html",
},
},
);
}