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

Commit 940202b

Browse files
committed
chore: update hello-world example
1 parent 07aacbe commit 940202b

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { APIRequest } from 'aleph/types.ts'
2+
3+
const global = globalThis as any
4+
5+
export default async function handler(req: APIRequest) {
6+
let count = global['__count'] || (global['__count'] = 0)
7+
switch (req.params.action) {
8+
case 'increase':
9+
count++
10+
global['__count'] = count
11+
req.json({ count })
12+
break
13+
case 'decrease':
14+
count--
15+
global['__count'] = count
16+
req.json({ count })
17+
break
18+
default:
19+
req.status(400).json({ error: 'UnknownAction', status: 400, message: `undefined acton '${req.params.action}'` })
20+
break
21+
}
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { APIRequest } from 'aleph/types.ts'
2+
3+
const global = globalThis as any
4+
5+
export default async function handler(req: APIRequest) {
6+
const count = global['__count'] || (global['__count'] = 0)
7+
req.json({ count })
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useCallback, useEffect, useState } from 'react'
2+
3+
export default function useCounter(): [number, boolean, () => void, () => void] {
4+
const [isSyncing, setIsSyncing] = useState(true)
5+
const [count, setCount] = useState(0)
6+
const increase = useCallback(() => {
7+
setCount(n => n + 1)
8+
fetch('/api/counter/increase').catch(e => console.error(e))
9+
}, [])
10+
const decrease = useCallback(() => {
11+
setCount(n => n - 1)
12+
fetch('/api/counter/decrease').catch(e => console.error(e))
13+
}, [])
14+
15+
useEffect(() => {
16+
fetch('/api/counter').then(resp => resp.json().catch(() => ({ count: 0 })))
17+
.then(({ count }) => {
18+
if (typeof count === 'number') {
19+
setCount(count)
20+
}
21+
})
22+
.catch(e => console.error(e))
23+
.finally(() => {
24+
setIsSyncing(false)
25+
})
26+
}, [])
27+
28+
return [count, isSyncing, increase, decrease]
29+
}

examples/hello-world/pages/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useDeno } from 'aleph'
2-
import React, { useState } from 'react'
2+
import React from 'react'
33
import Logo from '../components/logo.tsx'
4+
import useCounter from '../lib/useCounter.ts'
45

56
export default function Home() {
6-
const [count, setCount] = useState(0)
7+
const [count, isSyncing, increase, decrease] = useCounter()
78
const version = useDeno(() => Deno.version.deno)
89

910
return (
@@ -22,9 +23,14 @@ export default function Home() {
2223
</p>
2324
<div className="counter">
2425
<span>Counter:</span>
25-
<strong>{count}</strong>
26-
<button onClick={() => setCount(n => n - 1)}>-</button>
27-
<button onClick={() => setCount(n => n + 1)}>+</button>
26+
{isSyncing && (
27+
<em>...</em>
28+
)}
29+
{!isSyncing && (
30+
<strong>{count}</strong>
31+
)}
32+
<button onClick={decrease}>-</button>
33+
<button onClick={increase}>+</button>
2834
</div>
2935
<p className="copyinfo">Built by Aleph.js in Deno {version}</p>
3036
</div>

examples/hello-world/style/index.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ main {
8787
border-color: #ccc;
8888
}
8989

90+
.counter em {
91+
display: inline-block;
92+
width: 48px;
93+
font-weight: 400;
94+
text-align: center;
95+
color: #999;
96+
}
97+
9098
.counter strong {
9199
display: inline-block;
92100
width: 48px;

0 commit comments

Comments
 (0)