-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
208 lines (198 loc) · 5.55 KB
/
App.tsx
File metadata and controls
208 lines (198 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* React Application
*
* Components that use the system via hooks with useSyncExternalStore.
* Notice how we don't manage any lifecycle here - we just use the resources.
*
* This example demonstrates React 18's useSyncExternalStore API for
* subscribing to external state sources (our Braided resources).
*/
import React, { useSyncExternalStore } from "react";
import { useResource } from "./system";
/**
* Counter Display Component
*
* Demonstrates useSyncExternalStore with a Braided resource.
* The counter resource provides subscribe() and getSnapshot() methods.
* React automatically re-renders when the counter changes.
*/
function CounterDisplay() {
const counter = useResource("counter");
// Subscribe to counter changes using React 18's useSyncExternalStore
const count = useSyncExternalStore(counter.subscribe, counter.getSnapshot);
return (
<div
style={{
padding: "20px",
border: "2px solid #3b82f6",
borderRadius: "8px",
}}
>
<h2>🔢 Counter</h2>
<p style={{ fontSize: "48px", margin: "20px 0" }}>{count}</p>
<div style={{ display: "flex", gap: "10px" }}>
<button
onClick={() => counter.increment()}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#3b82f6",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Increment
</button>
<button
onClick={() => counter.reset()}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#ef4444",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Reset
</button>
</div>
</div>
);
}
/**
* Logger Display Component
*
* Demonstrates accessing multiple resources and their interactions.
* Both logger and counter use useSyncExternalStore for reactivity.
*/
function LoggerDisplay() {
const logger = useResource("logger");
// Subscribe to logger changes
const logs = useSyncExternalStore(logger.subscribe, logger.getSnapshot);
return (
<div
style={{
padding: "20px",
border: "2px solid #10b981",
borderRadius: "8px",
}}
>
<h2>📝 Logger</h2>
<div style={{ display: "flex", gap: "10px", marginBottom: "20px" }}>
<button
onClick={() => logger.log("Hello from React!")}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#10b981",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Log Message
</button>
<button
onClick={() => logger.logCount()}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#8b5cf6",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Log Count
</button>
<button
onClick={() => logger.clear()}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#ef4444",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Clear
</button>
</div>
<div
style={{
backgroundColor: "#1f2937",
color: "#10b981",
padding: "15px",
borderRadius: "4px",
fontFamily: "monospace",
fontSize: "14px",
maxHeight: "200px",
overflowY: "auto",
}}
>
{logs.length === 0 ? (
<div style={{ color: "#6b7280" }}>No logs yet...</div>
) : (
logs.map((log, i) => <div key={i}>{log}</div>)
)}
</div>
</div>
);
}
/**
* Main App Component
*/
export function App() {
return (
<div style={{ padding: "40px", maxWidth: "800px", margin: "0 auto" }}>
<h1 style={{ marginBottom: "10px" }}>
🧶 Braided React - useSyncExternalStore
</h1>
<p style={{ color: "#6b7280", marginBottom: "40px" }}>
Using React 18's useSyncExternalStore to subscribe to Braided resources.
No useState, no forceUpdate - just pure reactive subscriptions.
</p>
<div style={{ display: "flex", flexDirection: "column", gap: "20px" }}>
<CounterDisplay />
<LoggerDisplay />
</div>
<div
style={{
marginTop: "40px",
padding: "20px",
backgroundColor: "#fef3c7",
borderRadius: "8px",
}}
>
<h3 style={{ marginTop: 0 }}>💡 Key Concepts:</h3>
<ol style={{ marginBottom: 0 }}>
<li>
<strong>useSyncExternalStore:</strong> React 18 API for subscribing
to external state
</li>
<li>
<strong>subscribe():</strong> Resources provide a subscribe method
that React calls
</li>
<li>
<strong>getSnapshot():</strong> Returns current state, called on
every render
</li>
<li>
<strong>Automatic re-renders:</strong> No useState or manual render
needed!
</li>
<li>Open DevTools console to see system lifecycle logs</li>
</ol>
</div>
</div>
);
}