dissidua/app/page.tsx

30 lines
727 B
TypeScript
Raw Permalink Normal View History

"use client"
import { useEffect, useId, useState } from "react";
2023-12-18 07:55:02 -08:00
export default function Home() {
const id = useId()
const [name, setName] = useState<string>('');
2023-12-18 07:55:02 -08:00
useEffect(() => {
if (typeof window !== "undefined" && window.localStorage) {
let name = localStorage.getItem("name");
console.log("Got name", name);
setName(name);
}
}, []);
2023-12-18 07:55:02 -08:00
function saveName(name: string) {
setName(name);
if (typeof window !== "undefined" && window.localStorage) {
localStorage.setItem("name", name);
}
}
2023-12-18 07:55:02 -08:00
return (
<main className="flex" >
<label htmlFor={id}>Name</label>
<input id={id} value={name} onInput={e => saveName(e.target.value)}></input>
2023-12-18 07:55:02 -08:00
</main>
)
}