variant with preact instead of vanilla js
parent
6e98443bb9
commit
a1502eb2da
File diff suppressed because it is too large
Load Diff
@ -1,27 +0,0 @@
|
||||
const counterElement = document.querySelector('#counter-value')
|
||||
const incrementButton = document.querySelector('#btn-increment')
|
||||
const decrementButton = document.querySelector('#btn-decrement')
|
||||
|
||||
function updateCounter(value) {
|
||||
counterElement.textContent = `Counter: ${value}`
|
||||
}
|
||||
|
||||
incrementButton.addEventListener('click', async () => {
|
||||
const res = await fetch('/api/increment', { method: 'POST' })
|
||||
const data = await res.json()
|
||||
updateCounter(data)
|
||||
})
|
||||
|
||||
decrementButton.addEventListener('click', async () => {
|
||||
const res = await fetch('/api/decrement', { method: 'POST' })
|
||||
const data = await res.json()
|
||||
updateCounter(data)
|
||||
})
|
||||
|
||||
async function main() {
|
||||
const res = await fetch('/api/value')
|
||||
const data = await res.json()
|
||||
updateCounter(data)
|
||||
}
|
||||
|
||||
main()
|
@ -0,0 +1,38 @@
|
||||
import { render } from 'preact'
|
||||
import { useState, useEffect } from 'preact/hooks'
|
||||
|
||||
const App = ({}) => {
|
||||
const [counter, setCounter] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
fetch('/api/value')
|
||||
.then(res => res.json())
|
||||
.then(count => setCounter(count))
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>{counter}</div>
|
||||
<button
|
||||
onClick={() => {
|
||||
fetch('/api/decrement', { method: 'POST' })
|
||||
.then(res => res.json())
|
||||
.then(count => setCounter(count))
|
||||
}}
|
||||
>
|
||||
Decrementa
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
fetch('/api/increment', { method: 'POST' })
|
||||
.then(res => res.json())
|
||||
.then(count => setCounter(count))
|
||||
}}
|
||||
>
|
||||
Incrementa
|
||||
</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
render(<App />, document.querySelector('#app'))
|
Loading…
Reference in New Issue