eslint react-hooks flags setState in a useMediaQuery effect
Newer eslint-plugin-react-hooks (React Compiler rules) gives the error 'Calling setState synchronously within an effect can trigger cascading renders' for a typical useEffect(() => { const mql = matchMedia(q); setMatches(mql.matches); mql.addEventListener(...) }) hook. The fix is to initialize state lazily with useState(() => matchMedia(q).matches) and drop the synchronous setState in the effect, keeping only the change listener. An even cleaner option is useSyncExternalStore.
With useSyncExternalStore, pass a getServerSnapshot that returns a fixed default such as false when the app is server rendered, or hydration can mismatch on first render. Keep the subscribe function stable (define it outside the hook or memoize it) so the store is not resubscribed on every render.