You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/src/blog/build-react-website-with-dart.md
+43-35Lines changed: 43 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,24 +13,28 @@ tags:
13
13
- web-development
14
14
---
15
15
16
-
Want to build React websites with Dart instead of JavaScript or TypeScript? The **dart_node_react** package makes this possible with full type safety and familiar React patterns. This tutorial walks you through building a React web application entirely in Dart.
16
+
What if you could build React apps without the existential dread of `undefined is not a function`? What if your types actually meant something at runtime? What if you never had to debug another `Cannot read property 'map' of null` error at 2 AM?
17
17
18
-
## Why Build React Apps with Dart?
18
+
Good news: you can. With **dart_node_react**, you write React applications entirely in Dart. Same React patterns you know. Real type safety you've been dreaming about.
19
19
20
-
React developers often wish TypeScript's types existed at runtime. Dart solves this problem. Types are checked at compile time *and* runtime. Null safety is sound. When you write Dart code, you know exactly what types you're working with.
20
+
## Why Dart? (Besides the Obvious Joy of Not Using JavaScript)
21
21
22
-
Flutter developers already know Dart. With dart_node_react, you can use those same skills to build React web applications. Share code between Flutter and React. Use one language across your entire stack.
22
+
Let's be honest. TypeScript was a massive improvement over JavaScript. But its types are like a bouncer who checks IDs at the door and then goes home. Once you're past the compiler, anything goes.
23
+
24
+
Dart takes a different approach. Types exist at runtime. Null safety is sound. When your code compiles, you know your `String` is actually a `String` and not secretly `undefined` wearing a fake mustache.
25
+
26
+
Already know Flutter? You already know Dart. Now you can use those same skills to build React web apps. One language. Full stack. No context switching between "Dart brain" and "TypeScript brain."
23
27
24
28
## Setting Up Your Project
25
29
26
-
Create a new Dart project for your React frontend:
30
+
Getting started takes about 30 seconds. Create a new Dart project:
27
31
28
32
```bash
29
33
mkdir my_react_app &&cd my_react_app
30
34
dart create -t package .
31
35
```
32
36
33
-
Add the required dependencies to your `pubspec.yaml`:
37
+
Add the dependencies to your `pubspec.yaml`:
34
38
35
39
```yaml
36
40
name: my_react_app
@@ -42,11 +46,11 @@ dependencies:
42
46
dart_node_react: ^0.1.0
43
47
```
44
48
45
-
Run `dart pub get` to install the packages.
49
+
Run `dart pub get`. Done. No webpack config. No babel. No 47 dev dependencies fighting each other.
46
50
47
-
## Your First React Component
51
+
## Your First Component
48
52
49
-
Create a file at `web/app.dart`. This is your application entry point:
53
+
Create `web/app.dart`. This is where the magic happens:
pEl('This React app is built entirely with Dart.'),
72
+
pEl('Look ma, no JavaScript!'),
69
73
],
70
74
);
71
75
}).toJS,
72
76
);
73
77
```
74
78
75
-
The `createElement` function wraps your component logic. Inside, you return React elements using helper functions like `div`, `h1`, and `pEl` (for paragraph elements).
79
+
The `createElement` function wraps your component logic. Inside, you return React elements using helper functions like `div`, `h1`, and `pEl`. It feels like React because it *is* React, just with better types.
76
80
77
-
## Managing State with Hooks
81
+
## State Management: useState Without the Guesswork
Common elements include `div`, `span`, `h1`-`h6`, `pEl`, `ul`, `li`, `button`, `input`, `form`, `header`, `footer`, `mainEl`, `section`, `nav`, and `article`.
245
+
You get `div`, `span`, `h1`-`h6`, `pEl`, `ul`, `li`, `button`, `input`, `form`, `header`, `footer`, `mainEl`, `section`, `nav`, `article`, and more. Everything you need to build real UIs.
242
246
243
247
## Compiling and Running
244
248
245
-
Create an HTML file at `web/index.html`:
249
+
Create `web/index.html`:
246
250
247
251
```html
248
252
<!DOCTYPE html>
@@ -260,17 +264,17 @@ Create an HTML file at `web/index.html`:
260
264
</html>
261
265
```
262
266
263
-
Compile your Dart code to JavaScript:
267
+
Compile your Dart to JavaScript:
264
268
265
269
```bash
266
270
dart compile js web/app.dart -o web/app.dart.js
267
271
```
268
272
269
-
Serve the `web` directory with any static file server and open it in your browser.
273
+
Serve the `web` directory and open it in your browser. That's it. Your React app is running, and you didn't write a single line of JavaScript.
270
274
271
-
## Complete Example: Task Manager
275
+
## Putting It Together: A Task Manager
272
276
273
-
Here's a complete task manager component demonstrating all concepts:
277
+
Here's a complete example combining everything you've learned:
State management, event handling, list rendering. All type-safe. All Dart.
344
+
345
+
## What's Next?
346
+
347
+
You've got the basics. Now go build something. Explore [more hooks](/api/dart_node_react/) like `useMemo` and `useCallback`. Check out the [full-stack example](https://github.com/AstroCodez/dart_node/tree/main/examples/frontend) with authentication, API integration, and WebSocket support.
340
348
341
-
You now have the foundation to build React websites with Dart. Explore the [dart_node_react documentation](/api/dart_node_react/) for more hooks like `useEffect`, `useMemo`, and `useCallback`. Check out the [examples repository](https://github.com/AstroCodez/dart_node/tree/main/examples/frontend) for a complete full-stack application with authentication, API calls, and WebSocket integration.
349
+
No more fighting with type coercion. No more `any` escape hatches. Just clean, type-safe React apps in a language that respects your time.
342
350
343
-
Start building type-safe React applications with Dart today.
351
+
Welcome to the future. It compiles to JavaScript, but at least you don't have to write it.
0 commit comments