Skip to content

Commit 12fa795

Browse files
update vite tut to add type support (#1651)
1 parent 868e197 commit 12fa795

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

examples/tutorials/create_react.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ task to run both the React app and the API server.
130130
In your `package.json` file, update the `scripts` field to include the
131131
following:
132132

133-
```jsonc
133+
```jsonc title="package.json"
134134
{
135135
"scripts": {
136136
"dev": "deno task dev:api & deno task dev:vite",
@@ -143,18 +143,46 @@ following:
143143
If you run `deno task dev` now and visit `localhost:8000/api/dinosaurs`, in your
144144
browser you should see a JSON response of all of the dinosaurs.
145145
146+
## React support in Deno
147+
148+
At this point, your IDE or editor may be showing you warnings about missing
149+
types in your project. Deno has built-in TypeScript support for React
150+
applications. To enable this, you'll need to configure your project with the
151+
appropriate type definitions and DOM libraries. Create or update your
152+
`deno.json` file with the following TypeScript compiler options:
153+
154+
```jsonc title="deno.json"
155+
"compilerOptions": {
156+
"types": [
157+
"react",
158+
"react-dom",
159+
"@types/react"
160+
],
161+
"lib": [
162+
"dom",
163+
"dom.iterable",
164+
"deno.ns"
165+
],
166+
"jsx": "react-jsx",
167+
"jsxImportSource": "react"
168+
}
169+
```
170+
146171
## Update the entrypoint
147172
148173
The entrypoint for the React app is in the `src/main.tsx` file. Ours is going to
149174
be very basic:
150175
151176
```tsx title="main.tsx"
152-
import ReactDOM from "react-dom/client";
153-
import App from "./App";
177+
import { StrictMode } from "react";
178+
import { createRoot } from "react-dom/client";
154179
import "./index.css";
180+
import App from "./App.tsx";
155181

156-
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
157-
<App />,
182+
createRoot(document.getElementById("root")!).render(
183+
<StrictMode>
184+
<App />
185+
</StrictMode>,
158186
);
159187
```
160188
@@ -176,8 +204,8 @@ component from `react-router-dom` and define the two routes:
176204
177205
```tsx title="App.tsx"
178206
import { BrowserRouter, Route, Routes } from "react-router-dom";
179-
import Index from "./pages/index";
180-
import Dinosaur from "./pages/Dinosaur";
207+
import Index from "./pages/index.tsx";
208+
import Dinosaur from "./pages/Dinosaur.tsx";
181209
import "./App.css";
182210

183211
function App() {
@@ -285,7 +313,7 @@ it in a paragraph:
285313
```tsx title="Dinosaur.tsx"
286314
import { useEffect, useState } from "react";
287315
import { Link, useParams } from "react-router-dom";
288-
import { Dino } from "../types";
316+
import { Dino } from "../types.ts";
289317

290318
export default function Dinosaur() {
291319
const { selectedDinosaur } = useParams();

0 commit comments

Comments
 (0)