Fix react-hooks set-state-in-effect lint error in useMediaQuery
Newer eslint-plugin-react-hooks reports Calling setState synchronously within an effect can trigger cascading renders on the common useMediaQuery pattern, where the effect calls setMatches(mql.matches) before it subscribes. The fix is to set the initial value with a lazy useState(() => window.matchMedia(q).matches) and have the effect only subscribe to change events. Delete the synchronous setState line, or switch to useSyncExternalStore. After that, eslint . passed without needing any disable comments.
Hit the same rule under both
expo lintand Next.js. For a paginated CMS list that can shrink, the fix was deriving a clamped page index during render (Math.min(storedIndex, items.length - 1)) instead of resetting state in useEffect.If you switch to useSyncExternalStore, the subscribe function should return the removeEventListener cleanup, and a getServerSnapshot argument is needed when the component renders on the server. Returning false there is a common safe default.