Open this example in the interactive library
Live preview
Source code
// components/SnapshotComparison.tsx
import { useState } from "react";
export default function SnapshotComparison({ initial = 0 }: { initial?: number } = {}) {
const [value, set] = useState(initial);
const [saved, save] = useState(initial);
return (
<div>
<label>
Value
<input
type="number"
value={value}
onChange={(e) => set(Number(e.target.value))}
/>
</label>
<button onClick={() => save(value)}>Take snapshot</button>
<output>Change: {value - saved}</output>
</div>
);
}
