JSXを使う
Deno@v0.20.0 (2019/10/8) からJSXがサポートされました
.tsxと.jsxで記述されたファイルはJSXとして解釈され実行されます
Denoデフォルトのtsconfig.jsonのオプションは、
jsx: react
jsxFactory: React.createElement
になっています
そのため、ブラウザJSと同様にjsxファイルではReactをimportすることでJSXが利用できるようになります
Reactはjspm.ioから読み込むのが(今の所)かんたんです
code:index.tsx
// @deno-types="https://denopkg.com/soremwar/deno_types/react/v16.13.1/react.d.ts"
import React from "https://dev.jspm.io/react";
// @deno-types="https://denopkg.com/soremwar/deno_types/react-dom/v16.13.1/server.d.ts"
import ReactDOMServer from "https://dev.jspm.io/react-dom/server";
const View: React.FC<{title: string}> = ({ title, children }) => (
<html>
<head>
<title>{title}</title>
</head>
<body>
{children}
</body>
</html>
);
console.log(ReactDOMServer.renderToString(<View title="deno">land</View>));
Reactの型定義ファイルがないので、まだ使うには不便がありそうです
Deno用に変換された型定義を公開してくださっている方がいますのでJavaScriptモジュールに型を適用する方法で動作させることができます
型定義がないためか現在だと.tsxだとコンパイルエラーで動作しなさそうですyuta0801.icon
Denoからそのまま使える型定義ファイルがあったので書き換えたのとコードが壊れてたので修正しましたkuuote.icon
Deno@v1.16.0でReact v17で導入された新しいJSXトランスフォームがサポートされましたuki00a.icon
code:jsx
/** @jsxImportSource https://esm.sh/preact@10.5.15 */
export function Hello(props) {
return (
<div>Hello, {props.name}</div>
)
}