Lint error for setState in an effect: use a lazy useState initializer
@sjsherr12▲ 0 net votes
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.