parrottjrs / think-write

A writing app that organizes, inspires, and keeps you accountable.

Home Page:https://parrottjrs.github.io/think-write/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GOAL: Persist the value to local storage

ecumene opened this issue · comments

commented
const [value, setValue] = useState(localstorage.getItem("..."))

useEffect(() => {
  localstorage.setItem("...", value);
}, [value]);

Sorry to lurk and nitpic, but this should probably be:

const [value, setValue] = useState(() => localstorage.getItem("..."))

by passing a arrow function instead of just passing in the get item, it will only call the function once on initial render of the component, not attempt to run getItem every time the component renders, even though this is only used to seed useState and isn't useful after the first render

see https://react.dev/reference/react/useState#parameters