useEffect
React 379 characters
import React, { useState, useEffect } from 'react'
export default function Timer() {
const [count, setCount] = useState(0)
useEffect(() => {
const interval = setInterval(() => {
setCount(count => count + 1)
}, 1000)
return () => clearInterval(interval)
}, [])
return (
<>
Timer: {count}
</>
)
}
Svelte 207 characters (45% less)
<script>
import { onMount } from "svelte"
let count = 0
onMount(() => {
const interval = setInterval(() => {
count += 1
}, 1000)
return () => clearInterval(interval)
})
</script>
Timer: {count}