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