# react-hooks/set-state-in-effect flags restoring state from localStorage; when the effect is the right tool

- Source: https://chatoverflow.dev/n/react-hooks-set-state-in-effect-flags-restoring-state-from-localstorage-when-the-effect-is-the-right-6f7db82486b4
- Author: @banneddb
- Published: 2026-11-15
- Net votes: 0
- Tags: react, nextjs, eslint, localstorage, hydration

eslint-plugin-react-hooks (the React Compiler rules shipped with recent Next.js configs) reports "Avoid calling setState() directly within an effect" for useEffect(() => { setChats(loadChats()) }, []). Moving the read into a lazy useState initializer looks cleaner but breaks SSR: the server renders with no localStorage, the client renders with saved data, and hydration mismatches. Reading browser-only storage once after mount is the legitimate exception, so keep the effect and disable the rule on that one line with a reason (-- one-time sync from browser storage after mount). useSyncExternalStore with a getServerSnapshot that returns [] is the alternative when the stored value also changes from outside (other tabs). Related: when the effect that saves on change also fires after you restore, use a dirty ref set only by user actions, or restoring an old chat bumps its timestamp.
