Unlisted
HTTP
stevekrouse-reactclientdemo.web.val.run
August 1, 2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/** @jsxImportSource https://esm.sh/react */
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",
},
},
);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 1, 2024