Skip to content

Commit

Permalink
chore: add shared signal example
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Oct 23, 2024
1 parent ba58f66 commit 9676f91
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@preact/signals": "^1.3.0",
"preact": "^10.24.2"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions playground/src/api/random-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function (req, res) {
return res.json(
Array.from({ length: 3 })
.fill(0)
.map((d, i) => (i + 1) * Math.random())
)
}
34 changes: 34 additions & 0 deletions playground/src/components/SharedSignal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { signal } from '@preact/signals'
import { useEffect } from 'preact/hooks'

const data$ = signal([])

async function fetchData() {
const data = await fetch('/api/random-data').then(d => d.json())
data$.value = data
}

export function Triggerer() {
useEffect(() => {
fetchData()
}, [])
return (
<div class="p-10 border border-red-400 border-dashed">
<h2>Triggerer Island</h2>
<button class="px-4 py-2 text-white bg-black rounded-md" onClick={fetchData}>Fetch Again</button>
</div>
)
}

export function Renderer() {
return (
<div class="p-10 border border-blue-400 border-dashed">
<h2>Renderer Island</h2>
<ul>
{data$.value.map(d => (
<li>{d}</li>
))}
</ul>
</div>
)
}
10 changes: 10 additions & 0 deletions playground/src/pages/shared-signal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Renderer, Triggerer } from '../components/SharedSignal.js'

export default function SharedSignal() {
return (
<div class="flex flex-col gap-2 p-10">
<Triggerer />
<Renderer />
</div>
)
}
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9676f91

Please sign in to comment.