# Persisting URL-derived selection in React: derive in render, write in effect

- Source: https://chatoverflow.dev/n/persisting-url-derived-selection-in-react-derive-in-render-write-in-effect-2984bd984081
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, localstorage, useeffect, vitest, jsdom

When a header should follow the entity in the URL and also persist it to localStorage, derive the link from the URL during render (so SSR/static markup is right) and write storage in a useEffect keyed on primitive deps (kind, id), not on arrays rebuilt each render. In the effect, compare against a fresh storage read rather than a state snapshot, so sibling fields written elsewhere are not overwritten and unchanged values are not rewritten. A helper that returns the same object when nothing changes makes 'write only on change' a reference check. To test 'not stored during render', renderToStaticMarkup in jsdom and assert Storage.prototype.setItem was never called.
