Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit c887365

Browse files
committed
chore(examples): update hello-world example
1 parent 94264ef commit c887365

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const global = globalThis as any
3+
const store = globalThis as any
44

55
export default async function handler(req: APIRequest) {
6-
let count = global['__count'] || (global['__count'] = 0)
6+
let count = store.$count || 0
7+
78
switch (req.params.action) {
89
case 'increase':
910
count++
10-
global['__count'] = count
11+
store.$count = count
1112
req.json({ count })
1213
break
1314
case 'decrease':
1415
count--
15-
global['__count'] = count
16+
store.$count = count
1617
req.json({ count })
1718
break
1819
default:
19-
req.status(400).json({ error: 'UnknownAction', status: 400, message: `undefined acton '${req.params.action}'` })
20+
req.status(400).json({
21+
error: 'UnknownAction',
22+
status: 400,
23+
message: `undefined acton '${req.params.action}'`
24+
})
2025
break
2126
}
2227
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { APIRequest } from 'aleph/types.ts'
22

3-
const global = globalThis as any
3+
const store = globalThis as any
44

55
export default async function handler(req: APIRequest) {
6-
const count = global['__count'] || (global['__count'] = 0)
7-
req.json({ count })
6+
req.json({ count: store.$count || 0 })
87
}

examples/hello-world/lib/useCounter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useCallback, useEffect, useState } from 'react'
22

33
export default function useCounter(): [number, boolean, () => void, () => void] {
4-
const [isSyncing, setIsSyncing] = useState(true)
54
const [count, setCount] = useState(0)
5+
const [isSyncing, setIsSyncing] = useState(true)
66
const increase = useCallback(() => {
77
setCount(n => n + 1)
88
fetch('/api/counter/increase').catch(e => console.error(e))
@@ -13,9 +13,9 @@ export default function useCounter(): [number, boolean, () => void, () => void]
1313
}, [])
1414

1515
useEffect(() => {
16-
fetch('/api/counter').then(resp => resp.json().catch(() => ({ count: 0 })))
16+
fetch('/api/counter').then(resp => resp.json().catch(() => ({})))
1717
.then(({ count }) => {
18-
if (typeof count === 'number') {
18+
if (typeof count === 'number' && !Number.isNaN(count)) {
1919
setCount(count)
2020
}
2121
})

0 commit comments

Comments
 (0)