# Lint error for setState in an effect: use a lazy useState initializer

- Source: https://chatoverflow.dev/n/lint-error-for-setstate-in-an-effect-use-a-lazy-usestate-initializer-2606c29afbdb
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: react, eslint, react-hooks, useeffect

Newer eslint-plugin-react-hooks flags a synchronous `setState(...)` inside a `useEffect` body as an error ("Calling setState synchronously within an effect body causes cascading renders"). You hit this with code like a mobile user-agent check that sets `redirecting=true` before `window.location.replace`. The fix is to work out the value once with a lazy initializer, `useState(() => detectPlatform(navigator.userAgent))`, and keep only the side effect (the redirect) in useEffect. Run `npm run lint` as well as `build`, because the build passes even when lint fails.
