Skip to content

Commit 4b03fd2

Browse files
committed
Make blog post more engaging and add humor
Add witty commentary while keeping technical accuracy. TypeScript jokes, developer pain points, and fun closing line. https://claude.ai/code/session_01VeoxZJHrTkkuwC3oABbwhN
1 parent 0787971 commit 4b03fd2

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

website/src/blog/build-react-website-with-dart.md

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ tags:
1313
- web-development
1414
---
1515

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?
1717

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.
1919

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)
2121

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."
2327

2428
## Setting Up Your Project
2529

26-
Create a new Dart project for your React frontend:
30+
Getting started takes about 30 seconds. Create a new Dart project:
2731

2832
```bash
2933
mkdir my_react_app && cd my_react_app
3034
dart create -t package .
3135
```
3236

33-
Add the required dependencies to your `pubspec.yaml`:
37+
Add the dependencies to your `pubspec.yaml`:
3438

3539
```yaml
3640
name: my_react_app
@@ -42,11 +46,11 @@ dependencies:
4246
dart_node_react: ^0.1.0
4347
```
4448
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.
4650

47-
## Your First React Component
51+
## Your First Component
4852

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:
5054

5155
```dart
5256
import 'dart:js_interop';
@@ -65,18 +69,18 @@ ReactElement App() => createElement(
6569
className: 'app',
6670
children: [
6771
h1('Hello from Dart!'),
68-
pEl('This React app is built entirely with Dart.'),
72+
pEl('Look ma, no JavaScript!'),
6973
],
7074
);
7175
}).toJS,
7276
);
7377
```
7478

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.
7680

77-
## Managing State with Hooks
81+
## State Management: useState Without the Guesswork
7882

79-
dart_node_react provides type-safe React hooks. The `useState` hook manages component state:
83+
Here's where Dart really shines. The `useState` hook returns a `StateHook<T>` with actual, honest-to-goodness type safety:
8084

8185
```dart
8286
ReactElement Counter() => createElement(
@@ -101,15 +105,17 @@ ReactElement Counter() => createElement(
101105
);
102106
```
103107

104-
The `useState` hook returns a `StateHook<T>` object with:
108+
Three ways to update state:
109+
110+
- `count.value` - read the current value
111+
- `count.set(5)` - set a new value directly
112+
- `count.setWithUpdater((old) => old + 1)` - update based on previous value
105113

106-
- `value` - the current state value
107-
- `set(newValue)` - replaces the state
108-
- `setWithUpdater((oldValue) => newValue)` - updates based on previous state
114+
No more `useState<number | undefined>(undefined)` gymnastics. Just `useState(0)`. The compiler knows it's an `int`.
109115

110-
## Handling User Input
116+
## Building Forms (The Part Everyone Dreads)
111117

112-
Building forms requires handling input events. Here's a login form example:
118+
Forms don't have to be painful. Here's a login form that actually works:
113119

114120
```dart
115121
ReactElement LoginForm() => createElement(
@@ -123,7 +129,6 @@ ReactElement LoginForm() => createElement(
123129
errorState.set('Please fill in all fields');
124130
return;
125131
}
126-
// Submit login request
127132
print('Logging in: ${emailState.value}');
128133
}
129134
@@ -158,11 +163,11 @@ ReactElement LoginForm() => createElement(
158163
);
159164
```
160165

161-
The `getInputValue` helper extracts the input value from change events. Call `.toDart` to convert the JavaScript string to a Dart string.
166+
The `getInputValue` helper extracts input values from events. Call `.toDart` to convert JavaScript strings to Dart strings. Clean and predictable.
162167

163168
## Side Effects with useEffect
164169

165-
Load data when components mount using `useEffect`:
170+
Need to fetch data when a component mounts? `useEffect` works exactly like you'd expect:
166171

167172
```dart
168173
ReactElement UserList() => createElement(
@@ -172,7 +177,6 @@ ReactElement UserList() => createElement(
172177
173178
useEffect(() {
174179
Future<void> loadUsers() async {
175-
// Simulate API call
176180
await Future.delayed(Duration(seconds: 1));
177181
usersState.set(['Alice', 'Bob', 'Charlie']);
178182
loadingState.set(false);
@@ -200,11 +204,11 @@ ReactElement UserList() => createElement(
200204
);
201205
```
202206

203-
Pass an empty list `[]` as the second argument to run the effect only on mount. Return a cleanup function or `null` if no cleanup is needed.
207+
Pass an empty list `[]` to run the effect only on mount. Return a cleanup function or `null` if you don't need cleanup. No surprises here.
204208

205-
## Building the HTML Structure
209+
## All Your Favorite HTML Elements
206210

207-
dart_node_react provides functions for all standard HTML elements:
211+
dart_node_react provides functions for every HTML element you need:
208212

209213
```dart
210214
ReactElement PageLayout() => createElement(
@@ -238,11 +242,11 @@ ReactElement PageLayout() => createElement(
238242
);
239243
```
240244

241-
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.
242246

243247
## Compiling and Running
244248

245-
Create an HTML file at `web/index.html`:
249+
Create `web/index.html`:
246250

247251
```html
248252
<!DOCTYPE html>
@@ -260,17 +264,17 @@ Create an HTML file at `web/index.html`:
260264
</html>
261265
```
262266

263-
Compile your Dart code to JavaScript:
267+
Compile your Dart to JavaScript:
264268

265269
```bash
266270
dart compile js web/app.dart -o web/app.dart.js
267271
```
268272

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.
270274

271-
## Complete Example: Task Manager
275+
## Putting It Together: A Task Manager
272276

273-
Here's a complete task manager component demonstrating all concepts:
277+
Here's a complete example combining everything you've learned:
274278

275279
```dart
276280
ReactElement TaskManager() => createElement(
@@ -336,8 +340,12 @@ ReactElement TaskManager() => createElement(
336340
);
337341
```
338342

339-
## Next Steps
343+
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.
340348

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.
342350

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

Comments
 (0)