Skip to content

Instantly share code, notes, and snippets.

@whomwah
Last active November 11, 2022 23:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whomwah/f192cddaa2f214edba05587b6c0256c4 to your computer and use it in GitHub Desktop.
Save whomwah/f192cddaa2f214edba05587b6c0256c4 to your computer and use it in GitHub Desktop.
Deno / React example
// @deno-types="https://deno.land/x/types/react/v16.13.1/react.d.ts"
import React from "https://dev.jspm.io/react@16.13.1";
// @deno-types="https://deno.land/x/types/react-dom/v16.13.1/server.d.ts"
import ReactDOMServer from "https://dev.jspm.io/react-dom@16.13.1/server";
import { opine } from "https://deno.land/x/opine@0.3.0/mod.ts"
export {
Request,
Response,
NextFunction,
} from "https://deno.land/x/opine@0.3.0/src/types.ts";
declare global {
namespace JSX {
interface IntrinsicElements {
button: React.ComponentProps<'button'>;
div: React.ComponentProps<'div'>;
h1: React.ComponentProps<'h1'>;
p: React.ComponentProps<'p'>;
}
}
}
const App = () => {
const [count, setCount] = (React).useState(0);
return (
<div>
<h1>Hello DenoLand! </h1>
<button onClick={() => setCount(count + 1)}> Click the 🦕</button>
<p>You clicked the 🦕 {count} times </p>
</div>
);
};
const app = opine();
const browserBundlePath = "/browser.js";
const js =
`import React from "https://dev.jspm.io/react@16.13.1";\nimport ReactDOM from "https://dev.jspm.io/react-dom@16.13.1";\nconst App = ${App};\nReactDOM.hydrate(React.createElement(App), document.body);`;
const html =
`<html><head><script type="module" src="${browserBundlePath}"></script><style>* { font-family: Helvetica; }</style></head><body>${
(ReactDOMServer as any).renderToString(<App />)
}</body></html>`;
app.use(browserBundlePath, (req, res, next) => {
res.type("application/javascript").send(js);
});
app.use("/", (req, res, next) => {
res.type("text/html").send(html);
});
app.listen({ port: 3000 });
console.log("Denoland listening at http://localhost:3000");
@airtonix
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment