0

As I understand it, calling the useState setter with the same value should not trigger a re-render. However, in this specific case, I'm getting the following error: "Too many re-renders. React limits the number of renders to prevent an infinite loop." Since the state value is the primitive false, I would expect setMyState not to trigger a render at all.

function App() {
  const [myState, setMyState] = useState(false);
  setMyState(false)
  return (
    <div>
      Some value
    </div>
  );
}

I know this code is a bit contrived - wouldn't expect to call a state setter here. Of course we can wrap this setter with a useEffect to only call it when some other dependent value changes.

But I would have expected this to not trigger a re-render anyway since the state is the same primitive value

moomoo
  • 101
  • 1
  • 2
  • I asked the same question a few months ago. Answer under this one explains it: [Updating state to the same state directly in the component body](https://stackoverflow.com/questions/74034014/updating-state-to-the-same-state-directly-in-the-component-body) – Konrad Jun 16 '23 at 19:09
  • @Konrad - this answers my question, thank you for the link! – moomoo Jun 16 '23 at 21:00

0 Answers0